How to get all active sessions in Spring 5 WebSocket API?

后端 未结 1 1827
醉梦人生
醉梦人生 2021-01-21 05:19

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

相关标签:
1条回答
  • 2021-01-21 06:17

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

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

    Here is a sample code:

    @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());
        }
    }
    
    0 讨论(0)
提交回复
热议问题