hyx 4066495b90 first commit | 5 luni în urmă | |
---|---|---|
.. | ||
src | 5 luni în urmă | |
README.md | 5 luni în urmă | |
pom.xml | 5 luni în urmă |
基于spring-boot-starter-websocket
配置,定制规范,提供调用工具。
支持单机模式和集群模式:
一个系统只需要一个服务引入即可,例如系统服务sys,其他服务不要重复添加依赖。
```xml
<dependency>
<groupId>com.c503.htphy.ddcs</groupId>
<artifactId>enhance-websocket</artifactId>
<version>${project.release.version}</version>
</dependency>
```
application.yml 配置(单机模式)
spring:
websocket.mode: single
application.yml 配置(集群模式)
spring:
websocket.mode: cluster
rabbitmq:
host: localhost
port: 5672
username: xxxx
password: xxxx
<script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.min.js"></script>
<script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.min.js"></script>
<script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.js"></script>
<script>
var url = 'http://localhost:36601/sys/ws'; //后端路径地址
var token = "7cf3bb72d2af44cbb11e971b72f6319a" //当前Token,用于校验是否可以连接
var currentUser = "abc"; //当前用户登录名称,用于订阅个人消息通道
url += "?Authorization=" + token;
var sock = new SockJS(url); //创建SockJS连接。
var stomp = Stomp.over(sock); //创建STOMP客户端实例
stomp.connect({username: currentUser}, function (frame) {
//订阅广播消息(特殊指定)
stomp.subscribe('/topic/broadcast', function (message) {
console.log("收到广播消息:" + message.body);
});
//订阅主题消息(业务指定)
stomp.subscribe('/topic/test', function (message) {
console.log("收到主题消息:" + message.body);
});
//订阅个人消息,abc是当前用户名
stomp.subscribe('/topic/user-' + currentUser, function (message) {
console.log("收到个人消息:" + message.body);
});
//测试发送: stomp.send("/topic/test",{},"Hello,it's web client");
}, function (error) {
console.log("连接失败");
//失败重连逻辑
});