找到你要的答案

Q:Spring websockets and group messages

Q:春天的WebSockets和组信息

I would like to implement a chat using Spring websockets. What will be the best way to implement groups?

  • Register a new UserDestinationMessageHandler for URLs like /group/{groupname}/queue/* and create a new UserDestinationResolver. Are there any examples for this?
  • Make the controller implement ApplicationListener<SessionConnectedEvent>. But how can I get the session attributes from the event?

In both options I was taking into account that I will register the group to which the user belongs into session attributes.

我想实现一个聊天用弹簧WebSockets。什么是最好的方式来实现组?

  • Register a new UserDestinationMessageHandler for URLs like /group/{groupname}/queue/* and create a new UserDestinationResolver. Are there any examples for this?
  • Make the controller implement ApplicationListener<SessionConnectedEvent>. But how can I get the session attributes from the event?

在这两个选项中,我考虑到我将注册用户属于会话属性的组。

answer1: 回答1:

I ended up creating a destination like "/topic/group/{name}/message" managed by message broker and have user subscribed to it. To send a message you can either send it to the same destination or you can send it to a destination like "/app/message" and have it forward it from there. The latter option has the advantage that you can filter the message.

I ended up creating a destination like "/topic/group/{name}/message" managed by message broker and have user subscribed to it. To send a message you can either send it to the same destination or you can send it to a destination like "/app/message" and have it forward it from there. The latter option has the advantage that you can filter the message.

spring-websocket