当前位置:   article > 正文

【Spring Boot】使用Websocket实现点对点通信_springboot @sendtouser

springboot @sendtouser

实现方法

1,@sendToUser

@MessageMapping("/test")
@SendToUser("/topic/test")
public List<JSONObject> addTest(List<JSONObject>){
	return list;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2,convertAndSendToUser方法

socketMessageTemplate.convertAndSendToUser("userId","/topic/test", sendMessage);
  • 1

3,以上两种方法在我的需求里都无法实现点对点通信,于是不得不转换了思路,使用如下方法

public void addTest(@Header String token){
socketMessageTemplate.convertAndSend("/topic/test/" + token, sendMessage);
}
  • 1
  • 2
  • 3

通过header注解成功拿到前端传过来的token,订阅地址加上token,使用广播的方式来实现伪单播通信,经测试,可以实现。特此记录

参考博文

1,Spring Boot系列20 Spring Websocket实现向指定的用户发送消息

2,spring websocket 使用@SendToUser

3,springboot集成websocket点对点推送、广播推送

4,Spring Boot整合WebSocket及Spring Security实例

5,websocket配合spring-security使用token认证

6,springboot中通过stomp方式来处理websocket及token权限鉴权相关

7,spring security 获取用户信息

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/524677
推荐阅读
相关标签
  

闽ICP备14008679号