org.springframework.web.socket.client.standard.StandardWebSocketClient Java Examples

The following examples show how to use org.springframework.web.socket.client.standard.StandardWebSocketClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Stomp.java    From WeEvent with Apache License 2.0 7 votes vote down vote up
private void testOverSockJS() throws InterruptedException {
    // sock js transport
    List<Transport> transports = new ArrayList<>(2);
    transports.add(new WebSocketTransport(new StandardWebSocketClient()));
    transports.add(new RestTemplateXhrTransport());

    SockJsClient sockjsClient = new SockJsClient(transports);
    WebSocketStompClient stompClient = new WebSocketStompClient(sockjsClient);

    // StringMessageConverter
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(taskScheduler); // for heartbeats

    stompClient.connect(brokerSockJS, getSockJSSessionHandlerAdapter());

    Thread.sleep(100000L);
}
 
Example #2
Source File: WebSocketConfigWebAppTest.java    From joal with Apache License 2.0 7 votes vote down vote up
@Test
public void shouldMapDestinationToMessageMappingWithDestinationPrefix() throws InterruptedException, ExecutionException, TimeoutException {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    final StompSession stompSession = stompClient.connect("ws://localhost:" + port + "/" + TestConstant.UI_PATH_PREFIX, new StompSessionHandlerAdapter() {
    }).get(10, TimeUnit.SECONDS);

    stompSession.send("/joal/global", null);
    verify(messagingCallback, timeout(1500).times(1)).global();

    stompSession.send("/joal/announce", null);
    verify(messagingCallback, timeout(1500).times(1)).announce();

    stompSession.send("/joal/config", null);
    verify(messagingCallback, timeout(1500).times(1)).config();

    stompSession.send("/joal/torrents", null);
    verify(messagingCallback, timeout(1500).times(1)).torrents();

    stompSession.send("/joal/speed", null);
    verify(messagingCallback, timeout(1500).times(1)).speed();
}
 
Example #3
Source File: WebSocketStompClientIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");

	this.wac = new AnnotationConfigWebApplicationContext();
	this.wac.register(TestConfig.class);
	this.wac.refresh();

	this.server = new TomcatWebSocketTestServer();
	this.server.setup();
	this.server.deployConfig(this.wac);
	this.server.start();

	WebSocketClient webSocketClient = new StandardWebSocketClient();
	this.stompClient = new WebSocketStompClient(webSocketClient);
	this.stompClient.setMessageConverter(new StringMessageConverter());
}
 
Example #4
Source File: QuotesWebSocketIntegrationTest.java    From bearchoke with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
	log.info("Setting up Quotes Web Socket Integration test....");
	port = SocketUtils.findAvailableTcpPort();

	server = new TomcatWebSocketTestServer(port);
	server.deployConfig(FrontendWebApplicationInitializer.class);
	server.start();

	loginAndSaveXAuthToken("harrymitchell", "HarryMitchell5!", headers);

	List<Transport> transports = new ArrayList<>();
	transports.add(new WebSocketTransport(new StandardWebSocketClient()));
	RestTemplateXhrTransport xhrTransport = new RestTemplateXhrTransport(new RestTemplate());
	xhrTransport.setRequestHeaders(headers);
	transports.add(xhrTransport);

	sockJsClient = new SockJsClient(transports);
       sockJsClient.setHttpHeaderNames("X-Auth-Token");
	log.info("Setup complete!");
}
 
Example #5
Source File: ServiceClient.java    From spring-boot-websocket-client with MIT License 6 votes vote down vote up
public static void main(String... argv) {
    WebSocketClient webSocketClient = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

    String url = "ws://127.0.0.1:8080/hello";
    StompSessionHandler sessionHandler = new MySessionHandler();
    stompClient.connect(url, sessionHandler);

    new Scanner(System.in).nextLine(); //Don't close immediately.
}
 
Example #6
Source File: Application.java    From spring-websocket-client with MIT License 6 votes vote down vote up
public static void main(String args[]) throws Exception
   {
WebSocketClient simpleWebSocketClient =
    new StandardWebSocketClient();
List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(simpleWebSocketClient));

SockJsClient sockJsClient = new SockJsClient(transports);
WebSocketStompClient stompClient =
    new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());

String url = "ws://localhost:9090/chat";
String userId = "spring-" +
    ThreadLocalRandom.current().nextInt(1, 99);
StompSessionHandler sessionHandler = new MyStompSessionHandler(userId);
StompSession session = stompClient.connect(url, sessionHandler)
    .get();
BufferedReader in =
    new BufferedReader(new InputStreamReader(System.in));
for (;;) {
    System.out.print(userId + " >> ");
    System.out.flush();
    String line = in.readLine();
    if ( line == null ) break;
    if ( line.length() == 0 ) continue;
    ClientMessage msg = new ClientMessage(userId, line);
    session.send("/app/chat/java", msg);
}
   }
 
Example #7
Source File: WebSocketStompClientIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");

	this.wac = new AnnotationConfigWebApplicationContext();
	this.wac.register(TestConfig.class);
	this.wac.refresh();

	this.server = new TomcatWebSocketTestServer();
	this.server.setup();
	this.server.deployConfig(this.wac);
	this.server.start();

	WebSocketClient webSocketClient = new StandardWebSocketClient();
	this.stompClient = new WebSocketStompClient(webSocketClient);
	this.stompClient.setMessageConverter(new StringMessageConverter());
}
 
Example #8
Source File: StompTest.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Before
public void before() throws Exception {
    log.info("=============================={}.{}==============================",
            this.getClass().getSimpleName(),
            this.testName.getMethodName());

    String brokerStomp = "ws://localhost:" + this.listenPort + "/weevent-broker/stomp";
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();

    this.stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    // MappingJackson2MessageConverter
    stompClient.setMessageConverter(new StringMessageConverter());
    stompClient.setTaskScheduler(taskScheduler); // for heartbeats

    this.header.setDestination(topic);
    this.header.set("eventId", WeEvent.OFFSET_LAST);
    this.header.set("groupId", WeEvent.DEFAULT_GROUP_ID);

    this.failure = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    this.stompSession = this.stompClient.connect(brokerStomp, new MyStompSessionHandler(latch, this.failure)).get();
    latch.await();
    this.stompSession.setAutoReceipt(true);
}
 
Example #9
Source File: AuthChannelInterceptorAdapterWebAppTest.java    From joal with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCallAuthServiceWhenUserTriesToConnect() throws InterruptedException, ExecutionException, TimeoutException {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    final StompHeaders stompHeaders = new StompHeaders();
    stompHeaders.add(AuthChannelInterceptorAdapter.USERNAME_HEADER, "john");
    stompHeaders.add(AuthChannelInterceptorAdapter.TOKEN_HEADER, TestConstant.UI_SECRET_TOKEN);

    stompClient.connect("ws://localhost:" + port + "/" + TestConstant.UI_PATH_PREFIX, new WebSocketHttpHeaders(), stompHeaders, new StompSessionHandlerAdapter() {
    }).get(10, TimeUnit.SECONDS);

    verify(authenticatorService, times(1)).getAuthenticatedOrFail("john", TestConstant.UI_SECRET_TOKEN);
}
 
Example #10
Source File: WebSocketStompClientIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	logger.debug("Setting up before '" + this.testName.getMethodName() + "'");

	this.wac = new AnnotationConfigWebApplicationContext();
	this.wac.register(TestConfig.class);
	this.wac.refresh();

	this.server = new TomcatWebSocketTestServer();
	this.server.setup();
	this.server.deployConfig(this.wac);
	this.server.start();

	WebSocketClient webSocketClient = new StandardWebSocketClient();
	this.stompClient = new WebSocketStompClient(webSocketClient);
	this.stompClient.setMessageConverter(new StringMessageConverter());
}
 
Example #11
Source File: SendMessageClientImpl.java    From simpleblockchain with Apache License 2.0 5 votes vote down vote up
@Override
public void process() {

    BlockchainWebSocketHandler handler = new BlockchainWebSocketHandler();
    WebSocketClient client = new StandardWebSocketClient();
    try {
        WebSocketSession session = client.doHandshake(handler, WS_URI).get();
        session.sendMessage(new TextMessage("Hello World test."));
        session.close();
    } catch (Exception e) {
        logger.info(e);
    }
}
 
Example #12
Source File: WebSocket.java    From football-events with MIT License 5 votes vote down vote up
public WebSocket(String url) {
    this.url = url;
    var transports = new ArrayList<Transport>(1);
    transports.add(new WebSocketTransport(new StandardWebSocketClient()));
    WebSocketClient webSocketClient = new SockJsClient(transports);
    client = new WebSocketStompClient(webSocketClient);
    client.setMessageConverter(new MappingJackson2MessageConverter());
}
 
Example #13
Source File: WebSecurityConfigWebAppTest.java    From joal with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPermitOnPrefixedUriForWebsocketHandshakeEndpoint() throws InterruptedException, ExecutionException, TimeoutException {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    final StompSession stompSession = stompClient.connect("ws://localhost:" + port + "/" + TestConstant.UI_PATH_PREFIX, new StompSessionHandlerAdapter() {
    }).get(10, TimeUnit.SECONDS);

    assertThat(stompSession.isConnected()).isTrue();
}
 
Example #14
Source File: WebSocketConfigWebAppTest.java    From joal with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToConnectToAppPrefix() throws InterruptedException, ExecutionException, TimeoutException {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    final StompSession stompSession = stompClient.connect("ws://localhost:" + port + "/" + TestConstant.UI_PATH_PREFIX, new StompSessionHandlerAdapter() {
    }).get(1000, TimeUnit.SECONDS);

    assertThat(stompSession.isConnected()).isTrue();
}
 
Example #15
Source File: WebSocketConfigWebAppTest.java    From joal with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotBeAbleToConnectWithoutAppPrefix() {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    assertThatThrownBy(() ->
            stompClient.connect("ws://localhost:" + port + "/", new StompSessionHandlerAdapter() {
            }).get(1000, TimeUnit.SECONDS)
    )
            .isInstanceOf(ExecutionException.class)
            .hasMessageContaining("The HTTP response from the server [404]");
}
 
Example #16
Source File: WebSocketConfigWebAppTest.java    From joal with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotMapDestinationToMessageMappingWithoutDestinationPrefix() throws InterruptedException, ExecutionException, TimeoutException {
    final WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());

    final StompSession stompSession = stompClient.connect("ws://localhost:" + port + "/" + TestConstant.UI_PATH_PREFIX, new StompSessionHandlerAdapter() {
    }).get(10, TimeUnit.SECONDS);

    stompSession.send("/global", null);
    Thread.sleep(1500);
    verify(messagingCallback, timeout(1500).times(0)).global();
}
 
Example #17
Source File: WebSocketProxyTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private WebSocketSession appendingWebSocketSession(String url, WebSocketHttpHeaders headers, StringBuilder response, int countToNotify)
        throws Exception {
    StandardWebSocketClient client = new StandardWebSocketClient();
    client.getUserProperties().put(SSL_CONTEXT_PROPERTY, HttpClientUtils.ignoreSslContext());
    URI uri = UriComponentsBuilder.fromUriString(url).build().encode().toUri();
    return client.doHandshake(appendResponseHandler(response, countToNotify), headers, uri).get(30000, TimeUnit.MILLISECONDS);
}
 
Example #18
Source File: WebSocketIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Iterable<Object[]> arguments() {
	return Arrays.asList(new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new JettyWebSocketClient()}
	});
}
 
Example #19
Source File: WebSocketConfigurationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Iterable<Object[]> arguments() {
	return Arrays.asList(new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new StandardWebSocketClient()}
	});
}
 
Example #20
Source File: StompWebSocketIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Object[][] arguments() {
	return new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new StandardWebSocketClient()}
	};
}
 
Example #21
Source File: ApplicationTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void run() {
	List<Transport> transports = new ArrayList<>(2);
	transports.add(new WebSocketTransport(new StandardWebSocketClient()));
	transports.add(new RestTemplateXhrTransport());

	SockJsClient sockJsClient = new SockJsClient(transports);
	ListenableFuture<WebSocketSession> wsSession = sockJsClient.doHandshake(this.webSocketHandler,
			"ws://localhost:" + this.port + "/sockjs");

	assertThatExceptionOfType(ExecutionException.class)
			.isThrownBy(() -> wsSession.get().sendMessage(new TextMessage("a")));
}
 
Example #22
Source File: StompClient.java    From tutorials with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    WebSocketClient client = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(client);

    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    StompSessionHandler sessionHandler = new MyStompSessionHandler();
    stompClient.connect(URL, sessionHandler);

    new Scanner(System.in).nextLine(); // Don't close immediately.
}
 
Example #23
Source File: WebSocketConfigurationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Iterable<Object[]> arguments() {
	return Arrays.asList(new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new StandardWebSocketClient()}
	});
}
 
Example #24
Source File: StompWebSocketIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Object[][] arguments() {
	return new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new StandardWebSocketClient()}
	};
}
 
Example #25
Source File: RsvpApplication.java    From -Data-Stream-Development-with-Apache-Spark-Kafka-and-Spring-Boot with MIT License 5 votes vote down vote up
@Bean
public ApplicationRunner initializeConnection(
    RsvpsWebSocketHandler rsvpsWebSocketHandler) {
        return args -> {
            WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

            rsvpsSocketClient.doHandshake(
                rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
        };
    }
 
Example #26
Source File: WebSocketHandshakeTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Iterable<Object[]> arguments() {
	return Arrays.asList(new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new JettyWebSocketClient()}
	});
}
 
Example #27
Source File: RsvpApplication.java    From -Data-Stream-Development-with-Apache-Spark-Kafka-and-Spring-Boot with MIT License 5 votes vote down vote up
@Bean
public ApplicationRunner initializeConnection(
        RsvpsWebSocketHandler rsvpsWebSocketHandler) {
    return args -> {
        WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

        rsvpsSocketClient.doHandshake(
                rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
    };
}
 
Example #28
Source File: WebsocketTest.java    From seppb with MIT License 5 votes vote down vote up
@Before
public void setup() {
	List<Transport> transports = new ArrayList<>();
	transports.add(new WebSocketTransport(new StandardWebSocketClient()));
	transports.add(new RestTemplateXhrTransport());
	this.sockJsClient = new SockJsClient(transports);
}
 
Example #29
Source File: WebSocketHandshakeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Iterable<Object[]> arguments() {
	return Arrays.asList(new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new JettyWebSocketClient()}
	});
}
 
Example #30
Source File: StompWebSocketIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Parameters(name = "server [{0}], client [{1}]")
public static Object[][] arguments() {
	return new Object[][] {
			{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
			{new TomcatWebSocketTestServer(), new StandardWebSocketClient()},
			{new UndertowTestServer(), new StandardWebSocketClient()}
	};
}