注意:请参阅问题底部的更新,了解我最终得出的结论.
我需要通过发送请求消息的Web套接字向请求发送多个响应,第一个快速响应,其他响应在数据验证之后(10到60秒之后,从多个并行线程).
我无法让后来的响应停止在所有打开的网络套接字上播放.如何让它们只发送到初始Web套接字?或者我应该使用Spring STOMP之外的东西(因为,老实说,我想要的是路由到各种功能的消息,我不需要或想要能够广播到其他网络套接字,所以我怀疑我可以写信息经销商自己,即使它正在重新发明轮子).
我没有使用Spring身份验证(这是在改进遗留代码中).
在初始返回消息中,我可以使用@SendToUser,即使我们没有用户,Spring也只将返回值发送到发送消息的websocket.(见这个问题).
但是,响应速度较慢,我认为我需要使用SimpMessagingTemplate.convertAndSendToUser(用户,目的地,消息),但我不能,因为我必须传入用户,而我无法弄清楚用户是什么用户使用SendToUser.我试图按照这个问题中的步骤进行操作,但是在未经过身份验证时没有使它工作(在这种情况下,principal.getName()返回null).
我已经为原型大大简化了这一点,所以不要担心同步线程或任何东西.我只是希望网络套接字正常工作.
这是我的控制器:
@Controller
public class TestSocketController
{
private SimpMessagingTemplate template;
@Autowired
public TestSocketController(SimpMessagingTemplate template)
{
this.template = template;
}
// This doesn't work because I need to pass something for the first parameter.
// If I just use convertAndSend, it broacasts the response to all browsers
void setResults(String ret)
{
template.convertAndSendToUser("", "/user/topic/testwsresponse", ret);
}
// this only sends "Testing Return" to the browser tab hooked to this …
Run Code Online (Sandbox Code Playgroud)