I code WebSocket application with help classical Spring 5 WebSocket API, i.e. without using of SockJS and STOMP. I have a problem to get all active http-sessions. I can get one current session, but how to get all active sessions? If I would use a classical Java API(JSR356), I would use a method: session.getOpenSessions() to get all opened sessions. But I can't find anything like this method in Spring 5 WebSocket API. How to get all active sessions?
//Configuration of WebSocket.
@Configuration
@EnableWebSocket
public class WebSocketConfig implements
WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//Map endpoint URL to handler's method.
//Add interceptor of HTTP-handshake to copy HTTP-session's attributes to WebSocket-session's attributes.
registry.addHandler(getWsEndpoint(), "/endpoint").addInterceptors(new HttpSessionHandshakeInterceptor());
}
@Bean
public WebSocketHandler getWsEndpoint() {
return new WsEndpoint();
}
}
My WebSocket endpoint:
// WebSocket endpoint class.
public class WsEndpoint extends TextWebSocketHandler {
public WsEndpoint(){}
public WebSocketMessage<String> wsMsg;
// This method will be called after successful websocket connection.
@Override
public void afterConnectionEstablished(WebSocketSession session)
throws java.lang.Exception {
wsMsg = new TextMessage(new String("Connection ok!"));
session.sendMessage(wsMsg);
}
// This method is called if message was recieved.
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
wsMsg = new TextMessage(new String("Message recieved!"));
session.sendMessage(wsMsg);
}
}