Spring 4 WebSocket发送到指定用户:convertAndSendToUser函数
来源:互联网 发布:键盘反应速度调节软件 编辑:程序博客网 时间:2024/06/13 01:41
有时推送需要推送到指定的用户,这时就需要使用@SengToUser
注解或者convertAndSendToUser
这个函数,需要注意的是,推送到特定用户不一定非要使用Spring Security这个框架来实现登录和访问控制,我在项目中使用的是Apache Shiro。
在用户登录之后,在Session里面设置相应的Principal:
UsernamePasswordToken token = new UsernamePasswordToken(phonenum, password); token.setRememberMe(true); currentUser.login(token); Session session = currentUser.getSession(); session.setAttribute("user_id", user_id); session.setAttribute("phonenum", phonenum);
然后就可以发送给指定用户了:
public void handleMessage(Baobiaoorder order) { log.info("收到来自RabbitMQ的消息:" + order.getOuttradeno());// operations.convertAndSend("/topic/pay-result", order); operations.convertAndSendToUser(order.getPhonenum(), "/queue/pay-result", order); }
其中order.getPhonenum()
便是Session里面的手机号,因为我是把手机号作为Shiro的Principal。此时消息会发送给/queue/pay-result
这个目的地,但是客户端需要订阅的是/user/queue/pay-result
。值得注意的是,按照惯例,最好把这种发送给特定用户的、非广播式的目的地设置为queue而不是topic。这样就能做到发送消息给指定的用户了。
2 2
- Spring 4 WebSocket发送到指定用户:convertAndSendToUser函数
- SpringBoot-WebSocket广播消息+单点消息(指定用户发送消息)
- spring websocket 模拟发送消息
- SpringBoot学习-(十四)SpringBoot中建立WebSocket连接(STOMP实现发送消息给指定用户)
- ASP发送邮件到指定地址函数_ASP E-mail发送函数
- spring-websocket 配置 websocket发送消息(群发)
- websocket经过F5无法发送到服务端
- 发送快捷方式到指定文件夹
- 备份指定文件到用户主目录,使用tar命令压缩并发送邮件
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- activemq的连接池,通过spring的JmsTemplate发送消息到指定的Destination
- 【极光推送】给指定用户发送消息
- opensns 指定用户发送微博
- spring websocket 利用注解接收和发送消息
- spring配置websocket并实现群发/单独发送消息
- spring配置websocket并实现群发/单独发送消息
- spring配置websocket并实现群发/单独发送消息
- spring boot+quartz+websocket+sockJs实现定时发送
- python笔记(一)
- PDO 简介——PDO中的三个类及其方法
- UE4 VR 模式下全屏解决办法
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- JavaScript三目运算符
- Spring 4 WebSocket发送到指定用户:convertAndSendToUser函数
- iOS-自定义MJRefresh下拉刷新动画
- 面向对象之内部类
- Apache配置虚拟主机
- shell 管理 uwsgi 进行重启 防止多个进行 导致服务器挂掉
- 如何做好机器人方向的毕业设计?
- 用mm编译APK时大量can't find referenced method的问题的解决
- linux 下安装jdk
- 销项税额