/** * Connect to the given WebSocket URL and notify the given * {@link org.springframework.messaging.simp.stomp.StompSessionHandler} * when connected on the STOMP level after the CONNECTED frame is received. * @param url the url to connect to * @param handler the session handler * @param uriVars the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, StompSessionHandler handler, Object... uriVars) { return connect(url, null, handler, uriVars); }
/** * An overloaded version of * {@link #connect(String, StompSessionHandler, Object...)} that also * accepts {@link WebSocketHttpHeaders} to use for the WebSocket handshake. * @param url the url to connect to * @param handshakeHeaders the headers for the WebSocket handshake * @param handler the session handler * @param uriVariables the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, @Nullable WebSocketHttpHeaders handshakeHeaders, StompSessionHandler handler, Object... uriVariables) { return connect(url, handshakeHeaders, null, handler, uriVariables); }
/** * An overloaded version of * {@link #connect(String, StompSessionHandler, Object...)} that also accepts * {@link WebSocketHttpHeaders} to use for the WebSocket handshake and * {@link StompHeaders} for the STOMP CONNECT frame. * @param url the url to connect to * @param handshakeHeaders headers for the WebSocket handshake * @param connectHeaders headers for the STOMP CONNECT frame * @param handler the session handler * @param uriVariables the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, @Nullable WebSocketHttpHeaders handshakeHeaders, @Nullable StompHeaders connectHeaders, StompSessionHandler handler, Object... uriVariables) { Assert.notNull(url, "'url' must not be null"); URI uri = UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables).encode().toUri(); return connect(uri, handshakeHeaders, connectHeaders, handler); }
@Test public void publishSubscribe() throws Exception { String url = "ws://127.0.0.1:" + this.server.getPort() + "/stomp"; TestHandler testHandler = new TestHandler("/topic/foo", "payload"); this.stompClient.connect(url, testHandler); assertTrue(testHandler.awaitForMessageCount(1, 5000)); assertThat(testHandler.getReceived(), containsInAnyOrder("payload")); }
@Override protected ListenableFuture<StompSession> doConnect(StompSessionHandler handler) { return ((WebSocketStompClient) this.stompClient).connect(this.url, this.handshakeHeaders, getConnectHeaders(), handler, this.uriVariables); }
/** * Connect to the given WebSocket URL and notify the given * {@link org.springframework.messaging.simp.stomp.StompSessionHandler} * when connected on the STOMP level after the CONNECTED frame is received. * @param url the url to connect to * @param handler the session handler * @param uriVars the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, StompSessionHandler handler, Object... uriVars) { return connect(url, null, handler, uriVars); }
/** * Connect to the given WebSocket URL and notify the given * {@link org.springframework.messaging.simp.stomp.StompSessionHandler} * when connected on the STOMP level after the CONNECTED frame is received. * @param url the url to connect to * @param handler the session handler * @param uriVars the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, StompSessionHandler handler, Object... uriVars) { return connect(url, null, handler, uriVars); }
/** * An overloaded version of * {@link #connect(String, StompSessionHandler, Object...)} that also * accepts {@link WebSocketHttpHeaders} to use for the WebSocket handshake. * @param url the url to connect to * @param handshakeHeaders the headers for the WebSocket handshake * @param handler the session handler * @param uriVariables the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, @Nullable WebSocketHttpHeaders handshakeHeaders, StompSessionHandler handler, Object... uriVariables) { return connect(url, handshakeHeaders, null, handler, uriVariables); }
/** * An overloaded version of * {@link #connect(String, StompSessionHandler, Object...)} that also * accepts {@link WebSocketHttpHeaders} to use for the WebSocket handshake. * @param url the url to connect to * @param handshakeHeaders the headers for the WebSocket handshake * @param handler the session handler * @param uriVariables the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, @Nullable WebSocketHttpHeaders handshakeHeaders, StompSessionHandler handler, Object... uriVariables) { return connect(url, handshakeHeaders, null, handler, uriVariables); }
public void connect() { try { serverSession = stompClient .connect(getUri().toString(), buildWebSocketHttpHeaders(), this) .get(); } catch (Exception e) { logger.error("Error connecting to web socket uri " + getUri(), e); throw new RuntimeException(e); } }
/** * An overloaded version of * {@link #connect(String, StompSessionHandler, Object...)} that also accepts * {@link WebSocketHttpHeaders} to use for the WebSocket handshake and * {@link StompHeaders} for the STOMP CONNECT frame. * @param url the url to connect to * @param handshakeHeaders headers for the WebSocket handshake * @param connectHeaders headers for the STOMP CONNECT frame * @param handler the session handler * @param uriVariables the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, @Nullable WebSocketHttpHeaders handshakeHeaders, @Nullable StompHeaders connectHeaders, StompSessionHandler handler, Object... uriVariables) { Assert.notNull(url, "'url' must not be null"); URI uri = UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables).encode().toUri(); return connect(uri, handshakeHeaders, connectHeaders, handler); }
/** * An overloaded version of * {@link #connect(String, StompSessionHandler, Object...)} that also accepts * {@link WebSocketHttpHeaders} to use for the WebSocket handshake and * {@link StompHeaders} for the STOMP CONNECT frame. * @param url the url to connect to * @param handshakeHeaders headers for the WebSocket handshake * @param connectHeaders headers for the STOMP CONNECT frame * @param handler the session handler * @param uriVariables the URI variables to expand into the URL * @return a ListenableFuture for access to the session when ready for use */ public ListenableFuture<StompSession> connect(String url, @Nullable WebSocketHttpHeaders handshakeHeaders, @Nullable StompHeaders connectHeaders, StompSessionHandler handler, Object... uriVariables) { Assert.notNull(url, "'url' must not be null"); URI uri = UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables).encode().toUri(); return connect(uri, handshakeHeaders, connectHeaders, handler); }
WebSocketClient transport = new StandardWebSocketClient(); WebSocketStompClient stompClient = new WebSocketStompClient(transport); MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); stompClient.setMessageConverter(converter); StompSessionHandler handler = new WSClient(); //custom implementation String url = "ws://{URL}/ws/websocket"; stompClient.connect(url, handler);
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. } }