Java Code Examples for org.springframework.web.socket.client.WebSocketClient

The following examples show how to use org.springframework.web.socket.client.WebSocketClient. 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
@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 3
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 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-analysis-note   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	MockitoAnnotations.initMocks(this);

	WebSocketClient webSocketClient = mock(WebSocketClient.class);
	this.stompClient = new TestWebSocketStompClient(webSocketClient);
	this.stompClient.setTaskScheduler(this.taskScheduler);
	this.stompClient.setStompSession(this.stompSession);

	this.webSocketHandlerCaptor = ArgumentCaptor.forClass(WebSocketHandler.class);
	this.handshakeFuture = new SettableListenableFuture<>();
	given(webSocketClient.doHandshake(this.webSocketHandlerCaptor.capture(), any(), any(URI.class)))
			.willReturn(this.handshakeFuture);
}
 
Example 6
Source Project: spring-analysis-note   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Test
public void heartbeatDefaultValue() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	assertArrayEquals(new long[] {0, 0}, stompClient.getDefaultHeartbeat());

	StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
	assertArrayEquals(new long[] {0, 0}, connectHeaders.getHeartbeat());
}
 
Example 7
Source Project: spring-analysis-note   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Test
public void heartbeatDefaultValueWithScheduler() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	stompClient.setTaskScheduler(mock(TaskScheduler.class));
	assertArrayEquals(new long[] {10000, 10000}, stompClient.getDefaultHeartbeat());

	StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
	assertArrayEquals(new long[] {10000, 10000}, connectHeaders.getHeartbeat());
}
 
Example 8
Source Project: spring-analysis-note   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Test
public void heartbeatDefaultValueSetWithoutScheduler() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	stompClient.setDefaultHeartbeat(new long[] {5, 5});
	try {
		stompClient.processConnectHeaders(null);
		fail("Expected IllegalStateException");
	}
	catch (IllegalStateException ex) {
		// ignore
	}
}
 
Example 9
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 10
Source Project: java-technology-stack   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	MockitoAnnotations.initMocks(this);

	WebSocketClient webSocketClient = mock(WebSocketClient.class);
	this.stompClient = new TestWebSocketStompClient(webSocketClient);
	this.stompClient.setTaskScheduler(this.taskScheduler);
	this.stompClient.setStompSession(this.stompSession);

	this.webSocketHandlerCaptor = ArgumentCaptor.forClass(WebSocketHandler.class);
	this.handshakeFuture = new SettableListenableFuture<>();
	when(webSocketClient.doHandshake(this.webSocketHandlerCaptor.capture(), any(), any(URI.class)))
			.thenReturn(this.handshakeFuture);
}
 
Example 11
Source Project: java-technology-stack   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Test
public void heartbeatDefaultValue() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	assertArrayEquals(new long[] {0, 0}, stompClient.getDefaultHeartbeat());

	StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
	assertArrayEquals(new long[] {0, 0}, connectHeaders.getHeartbeat());
}
 
Example 12
Source Project: java-technology-stack   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Test
public void heartbeatDefaultValueWithScheduler() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	stompClient.setTaskScheduler(mock(TaskScheduler.class));
	assertArrayEquals(new long[] {10000, 10000}, stompClient.getDefaultHeartbeat());

	StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
	assertArrayEquals(new long[] {10000, 10000}, connectHeaders.getHeartbeat());
}
 
Example 13
Source Project: java-technology-stack   Source File: WebSocketStompClientTests.java    License: MIT License 5 votes vote down vote up
@Test
public void heartbeatDefaultValueSetWithoutScheduler() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	stompClient.setDefaultHeartbeat(new long[] {5, 5});
	try {
		stompClient.processConnectHeaders(null);
		fail("Expected IllegalStateException");
	}
	catch (IllegalStateException ex) {
		// ignore
	}
}
 
Example 14
@Bean
public ApplicationRunner initializeConnection(
        RsvpsWebSocketHandler rsvpsWebSocketHandler) {
    return args -> {
        WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

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

            rsvpsSocketClient.doHandshake(
                rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
        };
    }
 
Example 16
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 17
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 18
@Bean
@ConditionalOnMissingBean(WebSocketStompClient.class)
public WebSocketStompClient stompClient(WebSocketClient webSocketClient, MessageConverter messageConverter,
                                        @Qualifier("proxyStompClientTaskScheduler") TaskScheduler taskScheduler) {
    int bufferSizeLimit = 1024 * 1024 * 8;

    WebSocketStompClient client = new WebSocketStompClient(webSocketClient);
    client.setInboundMessageSizeLimit(bufferSizeLimit);
    client.setMessageConverter(messageConverter);
    client.setTaskScheduler(taskScheduler);
    client.setDefaultHeartbeat(new long[]{0, 0});
    return client;
}
 
Example 19
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 20
@Before
public void setUp() throws Exception {
	MockitoAnnotations.initMocks(this);

	WebSocketClient webSocketClient = mock(WebSocketClient.class);
	this.stompClient = new TestWebSocketStompClient(webSocketClient);
	this.stompClient.setTaskScheduler(this.taskScheduler);
	this.stompClient.setStompSession(this.stompSession);

	this.webSocketHandlerCaptor = ArgumentCaptor.forClass(WebSocketHandler.class);
	this.handshakeFuture = new SettableListenableFuture<>();
	when(webSocketClient.doHandshake(this.webSocketHandlerCaptor.capture(), any(), any(URI.class)))
			.thenReturn(this.handshakeFuture);
}
 
Example 21
@Test
public void heartbeatDefaultValue() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	assertArrayEquals(new long[] {0, 0}, stompClient.getDefaultHeartbeat());

	StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
	assertArrayEquals(new long[] {0, 0}, connectHeaders.getHeartbeat());
}
 
Example 22
@Test
public void heartbeatDefaultValueWithScheduler() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	stompClient.setTaskScheduler(mock(TaskScheduler.class));
	assertArrayEquals(new long[] {10000, 10000}, stompClient.getDefaultHeartbeat());

	StompHeaders connectHeaders = stompClient.processConnectHeaders(null);
	assertArrayEquals(new long[] {10000, 10000}, connectHeaders.getHeartbeat());
}
 
Example 23
@Test
public void heartbeatDefaultValueSetWithoutScheduler() throws Exception {
	WebSocketStompClient stompClient = new WebSocketStompClient(mock(WebSocketClient.class));
	stompClient.setDefaultHeartbeat(new long[] {5, 5});
	try {
		stompClient.processConnectHeaders(null);
		fail("Expected exception");
	}
	catch (IllegalArgumentException ex) {
		// Ignore
	}
}
 
Example 24
Source Project: tutorials   Source File: StompClient.java    License: 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 25
Source Project: spring-analysis-note   Source File: WebSocketTransport.java    License: MIT License 4 votes vote down vote up
public WebSocketTransport(WebSocketClient webSocketClient) {
	Assert.notNull(webSocketClient, "WebSocketClient is required");
	this.webSocketClient = webSocketClient;
}
 
Example 26
Source Project: spring-analysis-note   Source File: WebSocketTransport.java    License: MIT License 4 votes vote down vote up
/**
 * Return the configured {@code WebSocketClient}.
 */
public WebSocketClient getWebSocketClient() {
	return this.webSocketClient;
}
 
Example 27
Source Project: spring-analysis-note   Source File: WebSocketStompClient.java    License: MIT License 4 votes vote down vote up
/**
 * Return the configured WebSocketClient.
 */
public WebSocketClient getWebSocketClient() {
	return this.webSocketClient;
}
 
Example 28
Source Project: spring-analysis-note   Source File: WebSocketStompClientTests.java    License: MIT License 4 votes vote down vote up
public TestWebSocketStompClient(WebSocketClient webSocketClient) {
	super(webSocketClient);
}
 
Example 29
Source Project: java-technology-stack   Source File: WebSocketTransport.java    License: MIT License 4 votes vote down vote up
public WebSocketTransport(WebSocketClient webSocketClient) {
	Assert.notNull(webSocketClient, "WebSocketClient is required");
	this.webSocketClient = webSocketClient;
}
 
Example 30
Source Project: java-technology-stack   Source File: WebSocketTransport.java    License: MIT License 4 votes vote down vote up
/**
 * Return the configured {@code WebSocketClient}.
 */
public WebSocketClient getWebSocketClient() {
	return this.webSocketClient;
}