繁体   English   中英

春季启动,WebSocket不会向指定用户发送通知

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

我在向指定用户发送通知时遇到这样的问题。 这是我的配置文件。

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

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

这是我的客户代码

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

}

该控制器处理所有与聊天相关的处理程序方法

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

伙计们似乎是Spring Boot Websocket领域的新手,请告诉我是否足以将通知发送给指定用户。 相反,当我执行上述客户端代码时,得到通知的是用户主体,而不是所需的用户。 我在这里做错了什么? 它说,春季启动将会话ID保留在后台,并用它来重命名发送通知的人。 但是似乎这里不是这种情况...

这是单击通知发送按钮时的浏览器日志。

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"}

问题是,下一个用户没有发送“ hmkhitaryan1 ”,而是向我发送了“ hmkhitaryan ”。

您可以使用convertandSend答复特定用户。 像这样 -

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

并在您的配置类中-

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

我遇到了与sendToUser相同的问题。 但这对我有用,希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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