how to capture connection event in my webSocket server with Spring 4?

前端 未结 3 504
北海茫月
北海茫月 2021-01-05 19:55

I did a simple web socket communication with spring 4,STOMP and sock.js, following this https://github.com/rstoyanchev/spring-websocket-portfolio and this http://assets.spri

3条回答
  •  無奈伤痛
    2021-01-05 20:47

    As I understand, the question with DISCONNECT event is not solved in this topic. Handshake interception gives you only connect info but not disconnect.

    I have achieved this with the interceptors of inbound channel:

    
        ...
        
            
                
            
        
    
    

    ...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

提交回复
热议问题