在Spring Websocket上向特定用户发送消息 [英] Sending message to specific user on Spring Websocket

查看:980
本文介绍了在Spring Websocket上向特定用户发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅将websocket消息从服务器发送给特定用户?

我的webapp具有spring安全设置并使用websocket。我在尝试将消息从服​​务器发送到特定用户时遇到了棘手的问题。

My webapp has spring security setup and uses websocket. I'm encountering tricky problem trying to send message from server to specific user only.

我从阅读手册来自我们可以做的服务器

My understanding from reading the manual is from the server we can do

simpMessagingTemplate.convertAndSend("/user/{username}/reply", reply);

在客户端:

stompClient.subscribe('/user/reply', handler);

但是我永远无法调用订阅回调。我尝试了很多不同的路径,但没有运气。

But I could never get the subscription callback invoked. I have tried many different path but no luck.

如果我将其发送到 / topic / reply ,它可以工作,但所有其他连接的用户都会收到它也是。

If I send it to /topic/reply it works but all other connected users will receive it too.

为了说明问题,我在github上创建了这个小项目: https://github.com/gerrytan/wsproblem

To illustrate the problem I've created this small project on github: https://github.com/gerrytan/wsproblem

重现步骤:

1)克隆并构建项目(确保你使用的是jdk 1.7和maven 3.1)

1) Clone and build the project (make sure you're using jdk 1.7 and maven 3.1)

$ git clone https://github.com/gerrytan/wsproblem.git
$ cd wsproblem
$ mvn jetty:run

2)导航到 http:// localhost:8080 ,使用bob / test或jim / test

2) Navigate to http://localhost:8080, login using either bob/test or jim/test

3)点击请求用户特定消息。预期:仅为此用户显示仅收到消息给我旁边显示消息hello {username},实际:未收到任何内容

3) Click "Request user specific msg". Expected: a message "hello {username}" is displayed next to "Received Message To Me Only" for this user only, Actual: nothing is received

推荐答案

哦,客户端无需了解当前用户,服务器会为您做到这一点。

Oh, client side no need to known about current user, server will do that for you.

在服务器端,使用以下方式向用户发送消息:

On server side, using following way to send message to an user:

simpMessagingTemplate.convertAndSendToUser(username, "/queue/reply", message);

注意:使用 queue ,不是主题,Spring总是使用 queue with sendToUser

Note: Using queue, not topic, Spring always using queue with sendToUser

在客户端

stompClient.subscribe("/user/queue/reply", handler);

解释

当任何websocket连接打开时,Spring会为它分配一个会话ID (不是 HttpSession ,为每个连接分配) 。当您的客户订阅以 / user / 开头的频道时,例如: / user / queue / reply ,服务器实例将订阅名为的队列队列/回复用户[会话ID]

When any websocket connection is open, Spring will assign it a session id (not HttpSession, assign per connection). And when your client subscribe to an channel start with /user/, eg: /user/queue/reply, your server instance will subscribe to a queue named queue/reply-user[session id]

使用发送消息时用户,例如:用户名为 admin
您将编写 simpMessagingTemplate.convertAndSendToUser(admin,/ queue / reply,message) ;

When use send message to user, eg: username is admin You will write simpMessagingTemplate.convertAndSendToUser("admin", "/queue/reply", message);

Spring将确定哪个会话ID 映射到用户管理。例如:它发现两个会话 wsxedc123 thnujm456 ,Spring会将其转换为2个目标队列/ reply-userwsxedc123 queue / reply-userthnujm456 ,它会将您的邮件与2个目的地发送给您的邮件经纪人。

Spring will determine which session id mapped to user admin. Eg: It found two session wsxedc123 and thnujm456, Spring will translate it to 2 destination queue/reply-userwsxedc123 and queue/reply-userthnujm456, and it send your message with 2 destinations to your message broker.

消息代理接收消息并将其提供回服务器实例,该实例持有与每个会话相对应的会话(WebSocket会话可由一个或多个服务器保存)。 Spring会将消息转换为 destination (例如: user / queue / reply )和 session id (例如: wsxedc123 )。然后,它将消息发送到相应的 Websocket会话

The message broker receive the messages and provide it back to your server instance that holding session corresponding to each session (WebSocket sessions can be hold by one or more server). Spring will translate the message to destination (eg: user/queue/reply) and session id (eg: wsxedc123). Then, it send the message to corresponding Websocket session

这篇关于在Spring Websocket上向特定用户发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆