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

查看:96
本文介绍了在 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) 单击请求用户特定的信息".预期:仅此用户的仅收到给我的消息"旁边会显示一条消息你好 {用户名}",实际:未收到任何信息

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,而不是topic,Spring 总是使用queuesendToUser

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

在客户端

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

解释

当任何 websocket 连接打开时,Spring 会为其分配一个 session id(不是 HttpSession,为每个连接分配).当您的客户端订阅以 /user/ 开头的频道时,例如:/user/queue/reply,您的服务器实例将订阅名为 queue 的队列/reply-user[会话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 将确定哪个 session id 映射到用户 admin.例如:它发现了两个会话 wsxedc123thnujm456,Spring 会将其转换为 2 个目的地 queue/reply-userwsxedc123queue/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 session

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天全站免登陆