如何在会话中向特定订阅发送 websocket 消息?

时间:2021-01-13 21:41:17

标签: java websocket spring-websocket stomp spring-messaging

我已经使用 Spring WebSocket 和 STOMP 实现了一个 WebSocket 服务器。单个会话中有多个订阅,我只想向特定订阅发送消息。

重现步骤:

  1. 客户端通过调用注册的 STOMP 端点连接到服务器。 enter image description here
  2. 客户端通过连接进行 2 次订阅。
SUBSCRIBE
country:germany
id:sub-0
destination:/user/queue/countryUpdates
SUBSCRIBE
country:france
id:sub-1
destination:/user/queue/countryUpdates
  1. 在 SimpUserRegistry 中有 1 个用户和 1 个会话,其中 2 个订阅。enter image description here

  2. 问题是,如果我向一个订阅发送消息,它也会发送给其他订阅。enter image description here

  3. 在发送消息时,我正在本机标头中添加订阅 ID,但它不起作用。

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
accessor.setNativeHeader("id", subscriptionId);
accessor.setNativeHeader("subscription", subscriptionId);
accessor.setSubscriptionId(subscriptionId);
accessor.setLeaveMutable(true);
messagingTemplate.convertAndSendToUser(simpUserId, REPLY_DESTINATION, message, accessor.getMessageHeaders());

我尝试过的:如果我将国家/地区添加到目的地,则每个订阅都有唯一的目的地,而不会有重复的消息。

我想使用 id/subscriptionId 来确定订阅并向该特定订阅发送消息。

0 个答案:

没有答案