简体   繁体   中英

Spring boot, WebSocket doesn't send notification to the specified user

I have such problem in sending notification to the specified user. This is my config file.

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws")
            .withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    //registry.enableSimpleBroker("/topic");
}

This one is my client code

$(document).ready(function () {
connect();

}

function connect() {
var socket = new SockJS('/ws');
stompClient = Stomp.over(socket);
stompClient.connect({}, function () {
    stompClient.subscribe("/user/queue/reply", function (notification) {
        console.log("------------------------ subscribed !!!!!!");
        notify(JSON.parse(notification.body).content);
    });
});

}

This controller handles all chat related handler methods

@MessageMapping("/message")
@SendToUser("/queue/reply")
public ChatMessage processMessageFromClient(@Payload ChatMessage chatMessage) throws Exception {
    return chatMessage;
}

Guys seems I am new in Spring boot websocket sphere, and please tell me whether or not it is enough for sending notification to the specified user. Instead, when I execute the aforementioned client code, it's me, the user principal, who gets notified, not the needed user. What have I done wrong here? It says, that spring boot keeps the session id under the hood, and uses it to resole who to send the notification. But seems it is not the case here...

Here is the browser log, when notification send button is clicked.

Opening Web Socket...
stomp.min.js:8 Web Socket Opened...
stomp.min.js:8 >>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
stomp.min.js:8 <<< CONNECTED
version:1.1
heart-beat:0,0
user-name:hmkhitaryan
stomp.min.js:8 connected to server undefined
stomp.min.js:8 >>> SUBSCRIBE
id:sub-0
destination:/user/queue/reply
stomp.min.js:8 >>> SEND
destination:/app/message
content-length:71
{"sender":"hmkhitaryan","content":"friend request","type":"FR_REQUEST"}
stomp.min.js:8 <<< MESSAGE
destination:/user/queue/reply
content-type:application/json;charset=UTF-8
subscription:sub-0
message-id:xgec4kaf-15
content-length:71
{"type":"FR_REQUEST","content":"friend request","sender":"hmkhitaryan"}

The thing is, that instead of sending " hmkhitaryan1 ", the next user, it sends to me, " hmkhitaryan ".

You can use convertandSend to reply to specific user. Like this -

@Controller
public class GreetingController {

@Autowired
private SimpMessageSendingOperations  template ;


@MessageMapping("/message")
public void greeting(@Payload String message, 
          Principal principal){

    template.convertAndSendToUser(<userName_youwant_tosend>,"/queue/queue1",message);
 }
}

and in your config class -

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic","/queue","/user");
    config.setApplicationDestinationPrefixes("/app");
    config.setUserDestinationPrefix("/user");
}

I was facing the same problem with sendToUser. But this worked for me.Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM