介绍

  • 在某个项目中用到了websocket的功能,发现需要配置的东西很多,索性就构建了一个基础的模块
  • 该模块只适用于简单的websocket的快速使用
  • 满足了以下功能
    • 消息发送
    • 频道订阅(无限制)
    • 单个用户不同浏览器session共享
    • Jwt鉴权
      • 2.0.4开始

        引入依赖

        1. <dependency>
        2. <groupId>cn.jdevelops</groupId>
        3. <artifactId>webs-socket-client</artifactId>
        4. <version>2.0.4</version>
        5. </dependency>
        6. <dependency>
        7. <groupId>cn.jdevelops</groupId>
        8. <artifactId>utils-jwt</artifactId>
        9. <version>2.0.4</version>
        10. </dependency>
        11. <dependency>
        12. <groupId>com.alibaba</groupId>
        13. <artifactId>fastjson</artifactId>
        14. <version>1.2.79</version>
        15. </dependency>

        使用

        无需配置,直接看接口示例

接口示例

继承默认接口 **SocketController.java** 自带的两个**Action**

  1. package cn.tannn.websocket.controller;
  2. import cn.jdevelops.websocket.client.controller.SocketController;
  3. import cn.tannn.websocket.service.AlarmWebSocketServer;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @author tn
  9. * @ClassName WebSocketController
  10. * @description websocket接口
  11. * @date 2020-12-24 15:53
  12. */
  13. @RestController
  14. @RequestMapping("websocket")
  15. public class WebSocketController implements SocketController {
  16. @Autowired
  17. private AlarmWebSocketServer alarmWebSocketServer;
  18. /**
  19. * 测试接口是否通畅
  20. * @return
  21. */
  22. @RequestMapping("/test01")
  23. public String testo01(){
  24. return "websocket";
  25. }
  26. /**
  27. * 使用 sendInfoByLikeKey 进行模糊匹配用户进行消息发送
  28. * 匹配 keyPrefix 开头的 websocket 用户 给他们发送消息 (keyPrefix用户1, keyPrefix用户2)
  29. * @param keyPrefix 主键前缀
  30. * @param message 消息
  31. */
  32. @RequestMapping("/sendInfoByLikeKey")
  33. public String sendInfoByLikeKey(String keyPrefix, String message){
  34. alarmWebSocketServer.sendInfoByLikeKey(keyPrefix, message);
  35. return "success";
  36. }
  37. }

提供一个模糊发送信息服务示例

自定义了一个模糊匹配用户进行消息发送接口 **sendInfoByLikeKey**

service

  1. package cn.tannn.websocket.service;
  2. /**
  3. * @author tn
  4. * @ClassName AlarmWebSocketServer
  5. * @description 自定义消息发送接口
  6. * @date 2020-12-24 15:58
  7. */
  8. public interface AlarmWebSocketServer {
  9. /**
  10. * 使用 sendInfoByLikeKey 进行模糊匹配用户进行消息发送
  11. * 匹配 keyPrefix 开头的 websocket 用户 给他们发送消息 (keyPrefix用户1, keyPrefix用户2)
  12. * @param keyPrefix 主键前缀
  13. * @param message 消息
  14. */
  15. public void sendInfoByLikeKey(String keyPrefix, String message);
  16. }

impl

  1. package cn.tannn.websocket.service.impl;
  2. import cn.jdevelops.websocket.core.service.WebSocketServer;
  3. import cn.tannn.websocket.service.AlarmWebSocketServer;
  4. import org.springframework.stereotype.Service;
  5. import javax.annotation.Resource;
  6. /**
  7. * @author tn
  8. * @ClassName AlarmWebSocketServerImpl
  9. * @description 自定义消息发送接口
  10. * @date 2020-12-24 15:59
  11. */
  12. @Service
  13. public class AlarmWebSocketServerImpl implements AlarmWebSocketServer {
  14. @Resource
  15. private WebSocketServer webSocketServer;
  16. @Override
  17. public void sendInfoByLikeKey(String keyPrefix, String message) {
  18. // 匹配 keyPrefix 开头的 websocket 用户 给他们发送消息 (keyPrefix用户1, keyPrefix用户2)
  19. webSocketServer.sendInfoByLikeKey(keyPrefix,message);
  20. }
  21. }

调式

WebSocket在线工具

http://www.easyswoole.com/wstool.html

测试接口 < 2.0.4

服务地址

  • ws://127.0.0.1:8080/socket/tanning123

    ws://127.0.0.1:port/context-path/socket/频道名

消息发送

127.0.0.1:8080/websocket/sendInfoByLikeKey?keyPrefix=tanning&message=测试

图文

image.png

鉴权演示 >=2.0.4

  1. 接口集
  2. 示例项目地址

固定了ws地址前缀

ws://127.0.0.1:8080/socket/y/频道名 ws://127.0.0.1:8080/socket/n/频道名

鉴权

ws://127.0.0.1:8080/socket/y/频道名

  1. ws://127.0.0.1:8080/socket/y/tanning123?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJ0YW4iLCJzZXgiOiLnlLciLCJleHAiOjE2NDU5Mzg1NDl9.x9rbDLZEHlnboH69wRKZfEuq0brrtODYfJ950PmXjQ0

    不鉴权

    ws://127.0.0.1:8080/socket/n/频道名

  2. ws://127.0.0.1:8080/socket/n/tanning123

示例项目地址

https://github.com/en-o/Jdevelops-Example/tree/main/WebSocket