我想了解convertAndSendToUser如何在Spring SockJS + Websocket框架中工作.
在客户端,我们将连接为
stompClient.connect(login, password, callback())
Run Code Online (Sandbox Code Playgroud)
这将导致连接请求与登录和密码的"Stomp凭证",例如,如果我们处理SessionConnectEvent http://www.sergialmar.com/2014/03/detect-websocket-connects-and-disconnects-in -spring-4 /
但我仍然不清楚这是否是服务器端向队列发送操作的"用户":
simpMessagingTemplate.convertAndSendToUser(username, "/queue/reply", message);
Run Code Online (Sandbox Code Playgroud)
我能得到的最接近的是阅读这个帖子向Spring Websocket上的特定用户发送消息,由Thanh Nguyen Van回答,但目前还不清楚.
基本上我需要做的是订阅一些客户端到同一主题,但在服务器上,发送不同的数据.客户可以提供用户标识符.
我想向特定客户发送通知.
例如用户名用户
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends
AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic", "/queue");
registry.setApplicationDestinationPrefixes("/app");
}
Run Code Online (Sandbox Code Playgroud)
调节器
@GetMapping("/notify")
public String getNotification(Principal principal) {
String username = "user";
notifications.increment();
logger.info("counter" + notifications.getCount() + "" + principal.getName());
// logger.info("usersend:"+sha.getUser().getName()) ; //user
template.convertAndSendToUser(principal.getName(), "queue/notification", notifications);
return "Notifications successfully sent to Angular !";
}
Run Code Online (Sandbox Code Playgroud)
客户端
角度服务
connect() {
let socket = new SockJs(`api/socket`);
let stompClient = Stomp.over(socket);
return stompClient;
} …Run Code Online (Sandbox Code Playgroud) 有没有澄清,如果我使用"简单代理",Spring Websocket + SockJS的/ topic,/ queue等之间有什么区别?例如,这里向Spring Websocket上的特定用户发送消息:当您的客户端订阅以/ user /开头的频道时,例如:/ user/queue/reply,您的服务器实例将订阅名为queue/reply-user的队列[会话ID]
我想以一种明确的方式理解这种转换背后的逻辑.
我有以下js代码:
stompClient.subscribe('/topic/clients', function (calResult) {
updateClientsTable(JSON.parse(calResult.body));
});
$.get("/clients", null);
Run Code Online (Sandbox Code Playgroud)
并遵循服务器代码(最后一行调用它):
@GetMapping(value = {"/clients"})
@ResponseBody
public void loadClients() {
brokerMessagingTemplate.convertAndSend("/topic/clients", clientService.getClientList());
}
Run Code Online (Sandbox Code Playgroud)
有时前端未命中的结果 $.get("/clients", null);
据我了解问题:在结果进入前端的那一刻,订阅不会发生.
如果把$.get("/clients", null);以下代码放在代码中 - 一切正常.
你能解释一下如何等待订阅吗?
我正在开发一个实时通知系统弹簧4使用内置的Message Broker中,和蹬过的WebSocket.
我希望能够根据用户名向特定用户发送消息.为了实现这个目标,我正在使用类的convertAndSendToUser方法org.springframework.messaging.simp.SimpMessagingTemplate,如下所示:
private final MessagingTemplate messagingTemplate;
@Autowired
public LRTStatusListener(SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
@Scheduled(fixedDelay=5000)
public void sendMessages(Principal principal)
messagingTemplate
.convertAndSendToUser(principal.getName(), "/horray", "Horray, " + principal.getName() + "!");
}
Run Code Online (Sandbox Code Playgroud)
作为配置:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/notifications").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic", "/queue", "/user");
}
}
Run Code Online (Sandbox Code Playgroud)
客户端(通过JavaScript),我应该通过指定用户名来订阅一个频道(根据另一个非常类似的问题:在Spring Websocket上向特定用户发送消息).
stompClient.subscribe('/user/' + …Run Code Online (Sandbox Code Playgroud) 我想听听您的意见,当没有用户会话(无状态,安静的Web应用程序)时,使用Spring和STOMP协议实现websocket队列的最佳选择是什么.
根据这个解释:https://stackoverflow.com/a/31577152/3076403我知道Spring将使用queue我们使用的simpMessagingTemplate.convertAndSendToUser(...)方法并传递与会话ID相关联的用户名.否则它将使用a topic,其中所有订阅的客户端最终将读取从服务器返回的相同消息.
就我而言,我有一个带有Angular UI部分的RESTful应用程序.我想在UI端实现进度条.最初,UI使用先前确定的给定ID将消息发送到服务器,例如:{id: 20}.
然后,UI必须监听返回的模型对特定ID的更改并计算内部处理的进度.消息返回如下所示:{id: 20, timeLeft: 30, totalTime: 60}
因此,对于将订阅服务器上的websocket频道的每个Angular应用程序,它应该给出不同的结果.
由于我在应用程序中没有会话并且必须在服务器上实现websocket队列,您是否知道使用Spring和STOMP协议的其他解决方案与我最近提出的解决方案不同:
弹簧: simpMessagingTemplate.convertAndSend("/ui/progress/"+id, ...)
UI ng-stomp: $stomp.subscribe("/ui/progress/"+id, function(...))
我知道这将是一个"假"主题,而不是排队.任何评论和想法赞赏.
我只是跟进从春天的指令发送回响应特定用户,而不是广播消息instead.but到底,无应答消息可被送回.
这是我的js代码:
var stompClient = null;
function setConnected(connected) {
document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected;
document.getElementById('conversationDiv').style.visibility =
connected ? 'visible': 'hidden';
document.getElementById('response').innerHTML = '';
}
function connect() {
var socket = new SockJS('reqsample');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('user/queue/resp', function(resp) {
var body = JSON.parse(resp.body);
showResp('cmid:' + body.cmid + ',reqName:' + body.reqName
+ ',custNo:' + body.custNo + ',qty:' + body.quantity);
});
stompClient.subscribe('user/queue/errors', function(resp) {
var body = JSON.parse(resp.body);
showResp(body);
});
});
}
function disconnect() …Run Code Online (Sandbox Code Playgroud)