Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用Spring Websocket向特定用户发送消息_Spring Boot_Spring Security_Spring Websocket_Stomp - Fatal编程技术网

Spring boot 使用Spring Websocket向特定用户发送消息

Spring boot 使用Spring Websocket向特定用户发送消息,spring-boot,spring-security,spring-websocket,stomp,Spring Boot,Spring Security,Spring Websocket,Stomp,我使用本教程中描述的spring设置了WebSocket:。我需要的是,我的服务器每5秒向特定用户发送一条消息。所以我首先做了这个: @Autowired 私有SimpMessagingTemplate; @计划(固定利率=5000) 公众假期{ template.convertAndSend(“/topic/greetings”,新问候语(“buff!”)); } 它是有效的。现在,为了只向特定用户发送消息,我更改了以下内容: @Scheduled(fixedRate=5000) 公众假期{

我使用本教程中描述的spring设置了WebSocket:。我需要的是,我的服务器每5秒向特定用户发送一条消息。所以我首先做了这个:

@Autowired
私有SimpMessagingTemplate;
@计划(固定利率=5000)
公众假期{
template.convertAndSend(“/topic/greetings”,新问候语(“buff!”));
}
它是有效的。现在,为了只向特定用户发送消息,我更改了以下内容:

@Scheduled(fixedRate=5000)
公众假期{
convertAndSendToUser(“MyName”,“队列/问候语”,新问候语(“buff!”);
}
在WebSocketConfig.java中添加队列:

public void配置MessageBroker(MessageBrokerRegistry配置){
config.enableSimpleBroker(“/topic”,“/queue”);
config.setApplicationDestinationPrefixes(“/app”);
}
更改GreetingController.java中的注释:

@MessageMapping(“/hello”)
@发件人(“/队列/问候语”)
公共问候语UserGreeting(HelloMessage消息,主体)引发异常{
Thread.sleep(1000);//模拟延迟
返回新的问候语(“你好,+htmlitls.htmlEscape(message.getName())+”!”;
}
并更改app.js中的连接功能:

var socket=newsockjs('/gs-guide-websocket');
stompClient=Stomp.over(套接字);
stompClient.connect({},函数(框架){
setConnected(true);
console.log('Connected:'+frame);
stompClient.subscribe('用户/队列/问候语',函数(问候语){
showGreeting(JSON.parse(greeting.body.content);
});
});
服务器使用spring引导安全性,我通过使用SimpUserRegistry查找所有用户来判断
MyName
是否正确。但不幸的是,我的代码不起作用。我已经试过了,但我不想让Spring区分会话和用户。我也看了这个,但没有帮助,因为链接不起作用

这是我的控制台日志:

2020-01-20 17:08:51.352 DEBUG 8736 --- [nboundChannel-3] .WebSocketAnnotationMethodMessageHandler : Searching methods to handle SEND /app/hello session=kvv0m1qm, lookupDestination='/hello'
2020-01-20 17:08:51.352 DEBUG 8736 --- [nboundChannel-3] .WebSocketAnnotationMethodMessageHandler : Invoking de.iteratec.iteraweb.controllers.GreetingController#UserGreeting[2 args]
2020-01-20 17:08:52.354 DEBUG 8736 --- [nboundChannel-3] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Hello, hey!"}
2020-01-20 17:08:54.882 DEBUG 8736 --- [MessageBroker-2] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Bufff!"}
2020-01-20 17:08:59.883 DEBUG 8736 --- [MessageBroker-4] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Bufff!"}

我错过更改了吗?

您能看到客户端成功订阅了您的端点吗


我认为您缺少客户端代码中的第一个
/
“用户/队列/问候语”应该是
“/user/queue/问候语”

嘿,彼得,是的!你是对的。这是我的错,救了我一天,谢谢你的兄弟