【发布时间】:2019-06-17 06:00:02
【问题描述】:
我有一个包含大量组的应用程序,其中我的服务器使用消息队列 (RabbitMQ) 来观察组并在 WebSocket 发生更改时向用户发布通知。我正在使用 Spring boot 及其受本指南启发的 WebSocket 实现:https://spring.io/guides/gs/messaging-stomp-websocket/
以下是 JavaScript 客户端订阅频道的示例:
var socket = new SockJS('http://localhost/ws');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/group/1/notification', function (message) {
// to something..
});
});
我的 Java Spring WebSocket 控制器有这个 broadcastNotification 方法将消息发送到 /topic/group/{groupId}/notification 通道。
@Controller
public class GroupController {
private SimpMessagingTemplate template;
@Autowired
public GroupController(SimpMessagingTemplate template) {
this.template = template;
}
public void broadcastNotification(int groupId, Notification notification) {
this.template.convertAndSend("/topic/group/." + tenantId + "/notification", Notification);
}
}
这很好,但考虑到性能,我希望我的业务逻辑只观察当前在 WebSocket 上订阅的组。
当客户订阅/topic/group/1/notification 或/topic/group/1/* 频道时,如何在我的服务器上收到通知?网络用户在浏览网页时会订阅和退订。
【问题讨论】:
标签: java spring spring-boot websocket