介绍
- 在某个项目中用到了websocket的功能,发现需要配置的东西很多,索性就构建了一个基础的模块
- 该模块只适用于简单的websocket的快速使用
- 满足了以下功能
- 消息发送
- 频道订阅(无限制)
- 单个用户不同浏览器session共享
- Jwt鉴权
- 2.0.4开始
引入依赖
<dependency><groupId>cn.jdevelops</groupId><artifactId>webs-socket-client</artifactId><version>2.0.4</version></dependency><dependency><groupId>cn.jdevelops</groupId><artifactId>utils-jwt</artifactId><version>2.0.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.79</version></dependency>
使用
无需配置,直接看接口示例
- 2.0.4开始
接口示例
继承默认接口
**SocketController.java**自带的两个**Action**
package cn.tannn.websocket.controller;import cn.jdevelops.websocket.client.controller.SocketController;import cn.tannn.websocket.service.AlarmWebSocketServer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @author tn* @ClassName WebSocketController* @description websocket接口* @date 2020-12-24 15:53*/@RestController@RequestMapping("websocket")public class WebSocketController implements SocketController {@Autowiredprivate AlarmWebSocketServer alarmWebSocketServer;/*** 测试接口是否通畅* @return*/@RequestMapping("/test01")public String testo01(){return "websocket";}/*** 使用 sendInfoByLikeKey 进行模糊匹配用户进行消息发送* 匹配 keyPrefix 开头的 websocket 用户 给他们发送消息 (keyPrefix用户1, keyPrefix用户2)* @param keyPrefix 主键前缀* @param message 消息*/@RequestMapping("/sendInfoByLikeKey")public String sendInfoByLikeKey(String keyPrefix, String message){alarmWebSocketServer.sendInfoByLikeKey(keyPrefix, message);return "success";}}
提供一个模糊发送信息服务示例
自定义了一个模糊匹配用户进行消息发送接口
**sendInfoByLikeKey**
service
package cn.tannn.websocket.service;/*** @author tn* @ClassName AlarmWebSocketServer* @description 自定义消息发送接口* @date 2020-12-24 15:58*/public interface AlarmWebSocketServer {/*** 使用 sendInfoByLikeKey 进行模糊匹配用户进行消息发送* 匹配 keyPrefix 开头的 websocket 用户 给他们发送消息 (keyPrefix用户1, keyPrefix用户2)* @param keyPrefix 主键前缀* @param message 消息*/public void sendInfoByLikeKey(String keyPrefix, String message);}
impl
package cn.tannn.websocket.service.impl;import cn.jdevelops.websocket.core.service.WebSocketServer;import cn.tannn.websocket.service.AlarmWebSocketServer;import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** @author tn* @ClassName AlarmWebSocketServerImpl* @description 自定义消息发送接口* @date 2020-12-24 15:59*/@Servicepublic class AlarmWebSocketServerImpl implements AlarmWebSocketServer {@Resourceprivate WebSocketServer webSocketServer;@Overridepublic void sendInfoByLikeKey(String keyPrefix, String message) {// 匹配 keyPrefix 开头的 websocket 用户 给他们发送消息 (keyPrefix用户1, keyPrefix用户2)webSocketServer.sendInfoByLikeKey(keyPrefix,message);}}
调式
WebSocket在线工具
测试接口 < 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=测试
图文
鉴权演示 >=2.0.4
固定了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/频道名
ws://127.0.0.1:8080/socket/y/tanning123?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJ0YW4iLCJzZXgiOiLnlLciLCJleHAiOjE2NDU5Mzg1NDl9.x9rbDLZEHlnboH69wRKZfEuq0brrtODYfJ950PmXjQ0
不鉴权
ws://127.0.0.1:8080/socket/n/频道名
ws://127.0.0.1:8080/socket/n/tanning123
示例项目地址
https://github.com/en-o/Jdevelops-Example/tree/main/WebSocket
