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

The following examples show how to use org.springframework.web.socket.client.standard.StandardWebSocketClient. These examples are extracted from open source projects. 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
@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 2
Source Project: WeEvent   Source File: StompTest.java    License: 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 3
Source Project: WeEvent   Source File: Stomp.java    License: Apache License 2.0 6 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 4
@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 5
Source Project: spring-boot-websocket-client   Source File: ServiceClient.java    License: 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
@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 7
Source Project: joal   Source File: WebSocketConfigWebAppTest.java    License: Apache License 2.0 6 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 8
@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 9
Source Project: bearchoke   Source File: QuotesWebSocketIntegrationTest.java    License: 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 10
Source Project: spring-analysis-note   Source File: WebSocketConfigurationTests.java    License: 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 11
@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 12
Source Project: spring-analysis-note   Source File: WebSocketHandshakeTests.java    License: 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 13
Source Project: seppb   Source File: WebsocketTest.java    License: 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 14
Source Project: WeEvent   Source File: Stomp.java    License: Apache License 2.0 5 votes vote down vote up
private void testOverWebSocket() throws InterruptedException {
    // standard web socket transport
    WebSocketClient webSocketClient = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);

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

    stompClient.connect(brokerStomp, getWebSocketSessionHandlerAdapter());

    Thread.sleep(100000L);
}
 
Example 15
Source Project: code   Source File: GreetingIntegrationTests.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
	List<Transport> transports = new ArrayList<>();
	transports.add(new WebSocketTransport(new StandardWebSocketClient()));
	this.sockJsClient = new SockJsClient(transports);

	this.stompClient = new WebSocketStompClient(sockJsClient);
	this.stompClient.setMessageConverter(new MappingJackson2MessageConverter());
}
 
Example 16
@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 17
@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 18
Source Project: java-technology-stack   Source File: WebSocketHandshakeTests.java    License: 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 19
@Bean
public ApplicationRunner initializeConnection(
        RsvpsWebSocketHandler rsvpsWebSocketHandler) {
    return args -> {
        WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

        rsvpsSocketClient.doHandshake(
                rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
    };
}
 
Example 20
@Bean
public ApplicationRunner initializeConnection(
    RsvpsWebSocketHandler rsvpsWebSocketHandler) {
        return args -> {
            WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

            rsvpsSocketClient.doHandshake(
                rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
        };
    }
 
Example 21
Source Project: football-events   Source File: WebSocket.java    License: 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 22
Source Project: api-layer   Source File: WebSocketProxyTest.java    License: 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 23
Source Project: spring-websocket-client   Source File: Application.java    License: MIT License 5 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 24
Source Project: joal   Source File: WebSecurityConfigWebAppTest.java    License: 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 25
Source Project: joal   Source File: WebSocketConfigWebAppTest.java    License: 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 26
Source Project: joal   Source File: WebSocketConfigWebAppTest.java    License: 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 27
Source Project: joal   Source File: WebSocketConfigWebAppTest.java    License: 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 28
Source Project: simpleblockchain   Source File: SendMessageClientImpl.java    License: 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 29
@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
@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()}
	});
}