如何在 Spring 5 WebSocket API 中获取所有活动会话? [英] How to get all active sessions in Spring 5 WebSocket API?

查看:124
本文介绍了如何在 Spring 5 WebSocket API 中获取所有活动会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在经典 Spring 5 WebSocket API 的帮助下编写 WebSocket 应用程序,即不使用 SockJS 和 STOMP.我在获取所有活动的 http 会话时遇到问题.我可以获得一个当前会话,但如何获取所有活动会话?如果我使用经典的 Java API(JSR356),我会使用一个方法:session.getOpenSessions() 来获取所有打开的会话.但是我在 Spring 5 WebSocket API 中找不到类似这种方法的东西.如何获取所有活动会话?

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();
}
}

我的 WebSocket 端点:

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);
    }
}

推荐答案

SimpUserRegistry (DefaultSimpUserRegistry) 跟踪连接的 websocket 用户.

The SimpUserRegistry (DefaultSimpUserRegistry) keeps track of connected websocket users.

方法 getUsers() 返回所有连接的用户及其会话.

The method getUsers() returns all the connected users along with their sessions.

这是一个示例代码:

@RestController
public class WebSocketController {

    private final SimpUserRegistry simpUserRegistry;

    public WebSocketController(SimpUserRegistry simpUserRegistry) {
        this.simpUserRegistry = simpUserRegistry;
    }

    @GetMapping("/ws/users")
    public List<String> connectedEquipments() {
        return this.simpUserRegistry
                .getUsers()
                .stream()
                .map(SimpUser::getName)
                .collect(Collectors.toList());
    }
}

这篇关于如何在 Spring 5 WebSocket API 中获取所有活动会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆