Spring WebSocket @SendToSession: send message to specific session

前端 未结 3 776
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 02:49

Is it possible to send a message to specific session?

I have an unauthenticated websocket between clients and a Spring servlet. I need to send an unsolicited message

3条回答
  •  误落风尘
    2020-11-28 03:15

    It is very complicated and in my opinion, isn't worth it. You need to create a subscription for every user (even unauthenticated ones) by their session id.

    Let's say that every user subscribes to a unique queue only for him:

    stompClient.subscribe('/session/specific' + uuid, handler);
    

    On the server, before the user subscribes you will need to notify and send a message for the specific session and save to a map:

        @MessageMapping("/putAnonymousSession/{sessionId}")
        public void start(@DestinationVariable sessionId) throws Exception {
            anonymousUserSession.put(key, sessionId);
        }
    

    After that, when you want to send message to the user you will need to:

    messagingTemplate.convertAndSend("/session/specific" + key); 
    

    But I don't really know what you are trying to do and how you will find the specific session (who is anonymous).

提交回复
热议问题