下面列出了怎么用org.springframework.messaging.simp.stomp.StompSessionHandler的API类实例代码及写法,或者点击链接到github查看源代码。
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.
}
private WebSocketHandler connect() {
this.stompClient.connect("/foo", mock(StompSessionHandler.class));
verify(this.stompSession).getSessionFuture();
verifyNoMoreInteractions(this.stompSession);
WebSocketHandler webSocketHandler = this.webSocketHandlerCaptor.getValue();
assertNotNull(webSocketHandler);
return webSocketHandler;
}
private WebSocketHandler connect() {
this.stompClient.connect("/foo", mock(StompSessionHandler.class));
verify(this.stompSession).getSessionFuture();
verifyNoMoreInteractions(this.stompSession);
WebSocketHandler webSocketHandler = this.webSocketHandlerCaptor.getValue();
assertNotNull(webSocketHandler);
return webSocketHandler;
}
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);
}
}
private WebSocketHandler connect() {
this.stompClient.connect("/foo", mock(StompSessionHandler.class));
verify(this.stompSession).getSessionFuture();
verifyNoMoreInteractions(this.stompSession);
WebSocketHandler webSocketHandler = this.webSocketHandlerCaptor.getValue();
assertNotNull(webSocketHandler);
return webSocketHandler;
}
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.
}
@Override
protected ConnectionHandlingStompSession createSession(StompHeaders headers, StompSessionHandler handler) {
return this.stompSession;
}
@Override
protected ConnectionHandlingStompSession createSession(StompHeaders headers, StompSessionHandler handler) {
return this.stompSession;
}
@Override
protected ConnectionHandlingStompSession createSession(StompHeaders headers, StompSessionHandler handler) {
return this.stompSession;
}
/**
* 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, WebSocketHttpHeaders, StompSessionHandler, Object...)}
* that accepts a fully prepared {@link java.net.URI}.
* @param url the url to connect to
* @param handshakeHeaders the headers for the WebSocket handshake
* @param connectHeaders headers for the STOMP CONNECT frame
* @param sessionHandler the STOMP session handler
* @return a ListenableFuture for access to the session when ready for use
*/
public ListenableFuture<StompSession> connect(URI url, @Nullable WebSocketHttpHeaders handshakeHeaders,
@Nullable StompHeaders connectHeaders, StompSessionHandler sessionHandler) {
Assert.notNull(url, "'url' must not be null");
ConnectionHandlingStompSession session = createSession(connectHeaders, sessionHandler);
WebSocketTcpConnectionHandlerAdapter adapter = new WebSocketTcpConnectionHandlerAdapter(session);
getWebSocketClient().doHandshake(adapter, handshakeHeaders, url).addCallback(adapter);
return session.getSessionFuture();
}
/**
* 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, WebSocketHttpHeaders, StompSessionHandler, Object...)}
* that accepts a fully prepared {@link java.net.URI}.
* @param url the url to connect to
* @param handshakeHeaders the headers for the WebSocket handshake
* @param connectHeaders headers for the STOMP CONNECT frame
* @param sessionHandler the STOMP session handler
* @return a ListenableFuture for access to the session when ready for use
*/
public ListenableFuture<StompSession> connect(URI url, @Nullable WebSocketHttpHeaders handshakeHeaders,
@Nullable StompHeaders connectHeaders, StompSessionHandler sessionHandler) {
Assert.notNull(url, "'url' must not be null");
ConnectionHandlingStompSession session = createSession(connectHeaders, sessionHandler);
WebSocketTcpConnectionHandlerAdapter adapter = new WebSocketTcpConnectionHandlerAdapter(session);
getWebSocketClient().doHandshake(adapter, handshakeHeaders, url).addCallback(adapter);
return session.getSessionFuture();
}
/**
* 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 URI variables to expand into the URL
* @return ListenableFuture for access to the session when ready for use
*/
public ListenableFuture<StompSession> connect(String url, WebSocketHttpHeaders handshakeHeaders,
StompHeaders connectHeaders, StompSessionHandler handler, Object... uriVariables) {
Assert.notNull(url, "uriTemplate 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, WebSocketHttpHeaders, StompSessionHandler, Object...)}
* that accepts a fully prepared {@link java.net.URI}.
* @param url the url to connect to
* @param handshakeHeaders the headers for the WebSocket handshake
* @param connectHeaders headers for the STOMP CONNECT frame
* @param sessionHandler the STOMP session handler
* @return ListenableFuture for access to the session when ready for use
*/
public ListenableFuture<StompSession> connect(URI url, WebSocketHttpHeaders handshakeHeaders,
StompHeaders connectHeaders, StompSessionHandler sessionHandler) {
Assert.notNull(url, "'uri' must not be null");
ConnectionHandlingStompSession session = createSession(connectHeaders, sessionHandler);
WebSocketTcpConnectionHandlerAdapter adapter = new WebSocketTcpConnectionHandlerAdapter(session);
getWebSocketClient().doHandshake(adapter, handshakeHeaders, url).addCallback(adapter);
return session.getSessionFuture();
}
/**
* 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);
}
/**
* 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);
}
/**
* 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 URI variables to expand into the URL
* @return 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 URI variables to expand into the URL
* @return ListenableFuture for access to the session when ready for use
*/
public ListenableFuture<StompSession> connect(String url, WebSocketHttpHeaders handshakeHeaders,
StompSessionHandler handler, Object... uriVariables) {
return connect(url, handshakeHeaders, null, handler, uriVariables);
}