Java 如何使用 Spring 4 在我的 webSocket 服务器中捕获连接事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21781667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to capture connection event in my webSocket server with Spring 4?
提问by Icaro
I did a simple web socket communication with spring 4,STOMP and sock.js, following this https://github.com/rstoyanchev/spring-websocket-portfolioand this http://assets.spring.io/wp/WebSocketBlogPost.html
我在这个https://github.com/rstoyanchev/spring-websocket-portfolio和这个 http://assets.spring.io/wp/WebSocketBlogPost之后,与 spring 4、STOMP 和 sock.js 做了一个简单的 web socket 通信。 html
well, I want to know if is possible capture connection events like when a new client was connected to my server or when a client was disconnected, is that possible in Spring 4.0.0?
好吧,我想知道是否可以捕获连接事件,例如新客户端何时连接到我的服务器或客户端何时断开连接,这在 Spring 4.0.0 中是否可能?
采纳答案by Angular University
This an be done with a connection handshake interceptor (HttpSessionHandshakeInterceptor), quoting the documentation:
这可以通过连接握手拦截器(HttpSessionHandshakeInterceptor)完成,引用文档:
The easiest way to customize the initial HTTP WebSocket handshake request is through a HandshakeInterceptor, which exposes "before" and "after" the handshake methods.
自定义初始 HTTP WebSocket 握手请求的最简单方法是通过 HandshakeInterceptor,它公开“之前”和“之后”握手方法。
回答by walv
As I understand, the question with DISCONNECT event is not solved in this topic. Handshake interception gives you only connect info but not disconnect.
据我了解,本主题未解决 DISCONNECT 事件的问题。握手拦截只为您提供连接信息,但不会断开连接。
I have achieved this with the interceptors of inbound channel:
我已经通过入站通道的拦截器实现了这一点:
<websocket:message-broker>
...
<websocket:client-inbound-channel>
<websocket:interceptors>
<bean class="com......MyChannelInterception"></bean>
</websocket:interceptors>
</websocket:client-inbound-channel>
</websocket:message-broker>
...and class...
……还有课……
import java.security.Principal;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
public class MyChannelInterception extends ChannelInterceptorAdapter {
private static final Logger LOGGER = LogManager.getLogger(WrcChannelInterception.class);
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
MessageHeaders headers = message.getHeaders();
SimpMessageType type = (SimpMessageType) headers.get("simpMessageType");
String simpSessionId = (String) headers.get("simpSessionId");
if (type == SimpMessageType.CONNECT) {
Principal principal = (Principal) headers.get("simpUser");
LOGGER.debug("WsSession " + simpSessionId + " is connected for user " + principal.getName());
} else if (type == SimpMessageType.DISCONNECT) {
LOGGER.debug("WsSession " + simpSessionId + " is disconnected");
}
return message;
}
}
Please note that Principal is available on CONNECT but not on DISCONNECT, however you have sweet Session ID
请注意 Principal 在 CONNECT 上可用,但在 DISCONNECT 上不可用,但是你有一个甜蜜的会话 ID
回答by Sergi Almar
Spring WebSocket publishes events when messages are received from the client, if you are using STOMP, these are the events published:
Spring WebSocket 在收到来自客户端的消息时发布事件,如果您使用的是 STOMP,这些是发布的事件:
- SessionConnectedEvent
- SessionConnectEvent
- SessionDisconnectEvent
- SessionSubscribeEvent
- SessionUnsubscribeEvent
The easiest way to detect connects and disconnects is by implementing an event listener for the mentioned events.
检测连接和断开连接的最简单方法是为上述事件实现一个事件侦听器。
public class WebSocketEventListener {
@EventListener
private void handleSessionConnected(SessionConnectEvent event) {
...
}
@EventListener
private void handleSessionDisconnect(SessionDisconnectEvent event) {
...
}
}
Here's a sample implementation that keeps track of connected users: https://github.com/salmar/spring-websocket-chat/blob/master/src/main/java/com/sergialmar/wschat/event/PresenceEventListener.java
这是一个跟踪连接用户的示例实现:https: //github.com/salmar/spring-websocket-chat/blob/master/src/main/java/com/sergialmar/wschat/event/PresenceEventListener.java