Spring websocket stomp client如何捕捉连接丢失?
Spring websocket stomp client how to capture the connection is lost?
我使用spring websocket stomp 客户端。下面是一段代码:
List<Transport> transports = new ArrayList<Transport>(2);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
transports.add(new RestTemplateXhrTransport());
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
headers.add("Cookie", client.getCookieString());
SockJsClient sockJsClient = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new StringMessageConverter());
ListenableFuture<StompSession> future =
stompClient.connect(configuration.getApp().getWebsocketServerBase() + "/websocket/sa", headers, new MyWebSocketHandler());
future.addCallback(new SuccessCallback<StompSession>() {
public void onSuccess(StompSession stompSession) {
System.out.println("on Success!");
}
}, new FailureCallback() {
public void onFailure(Throwable throwable) {
System.out.println("on Failure!");
}
});
可以,但是当websocket服务器关闭时,客户端收不到消息。
如何监听服务器关闭事件?
我找到了解决方案。
MyWebSocketHandler 像这样实现 StompSessionHandler:
private class MyWebSocketHandler implements StompSessionHandler {
@Override
public void afterConnected(StompSession stompSession, StompHeaders stompHeaders) {
}
@Override
public void handleException(StompSession stompSession, StompCommand stompCommand, StompHeaders stompHeaders, byte[] bytes, Throwable throwable) {
}
@Override
public void handleTransportError(StompSession stompSession, Throwable throwable) {
if (throwable instanceof ConnectionLostException) {
// if connection lost, call this
}
}
@Override
public Type getPayloadType(StompHeaders stompHeaders) {
return null;
}
@Override
public void handleFrame(StompHeaders stompHeaders, Object o) {
}
}
你可以看到方法handleTransportError。谢谢。
参考Spring WebSocket Document 25.4.13 STOMP客户端。
我想你也能赶上SessionDisconnectEvent:
SessionDisconnectEvent
— published when a STOMP session ends. The DISCONNECT may have been sent from the client, or it may also be automatically generated when the WebSocket session is closed. In some cases this event may be published more than once per session. Components should be idempotent with regard to multiple disconnect events.
我使用spring websocket stomp 客户端。下面是一段代码:
List<Transport> transports = new ArrayList<Transport>(2);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
transports.add(new RestTemplateXhrTransport());
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
headers.add("Cookie", client.getCookieString());
SockJsClient sockJsClient = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new StringMessageConverter());
ListenableFuture<StompSession> future =
stompClient.connect(configuration.getApp().getWebsocketServerBase() + "/websocket/sa", headers, new MyWebSocketHandler());
future.addCallback(new SuccessCallback<StompSession>() {
public void onSuccess(StompSession stompSession) {
System.out.println("on Success!");
}
}, new FailureCallback() {
public void onFailure(Throwable throwable) {
System.out.println("on Failure!");
}
});
可以,但是当websocket服务器关闭时,客户端收不到消息。
如何监听服务器关闭事件?
我找到了解决方案。
MyWebSocketHandler 像这样实现 StompSessionHandler:
private class MyWebSocketHandler implements StompSessionHandler {
@Override
public void afterConnected(StompSession stompSession, StompHeaders stompHeaders) {
}
@Override
public void handleException(StompSession stompSession, StompCommand stompCommand, StompHeaders stompHeaders, byte[] bytes, Throwable throwable) {
}
@Override
public void handleTransportError(StompSession stompSession, Throwable throwable) {
if (throwable instanceof ConnectionLostException) {
// if connection lost, call this
}
}
@Override
public Type getPayloadType(StompHeaders stompHeaders) {
return null;
}
@Override
public void handleFrame(StompHeaders stompHeaders, Object o) {
}
}
你可以看到方法handleTransportError。谢谢。
参考Spring WebSocket Document 25.4.13 STOMP客户端。
我想你也能赶上SessionDisconnectEvent:
SessionDisconnectEvent
— published when a STOMP session ends. The DISCONNECT may have been sent from the client, or it may also be automatically generated when the WebSocket session is closed. In some cases this event may be published more than once per session. Components should be idempotent with regard to multiple disconnect events.