hyx 4066495b90 first commit 5 luni în urmă
..
src 4066495b90 first commit 5 luni în urmă
README.md 4066495b90 first commit 5 luni în urmă
pom.xml 4066495b90 first commit 5 luni în urmă

README.md

Websocket 增强功能使用说明

基于spring-boot-starter-websocket配置,定制规范,提供调用工具。

支持单机模式和集群模式:

  • 单机模式消息管理由本地内存处理,仅支持单机应用方式
  • 集群模式依赖于RabbitMQ(或其他消息中间件)作为消息代理,利用其订阅机制,支持应用集群部署

使用方法(服务端)

  1. pom.xml 引入依赖

一个系统只需要一个服务引入即可,例如系统服务sys,其他服务不要重复添加依赖。

```xml
<dependency>
    <groupId>com.c503.htphy.ddcs</groupId>
    <artifactId>enhance-websocket</artifactId>
    <version>${project.release.version}</version>
</dependency>
```
  1. application.yml 配置(单机模式)

    spring:
      websocket.mode: single
    
  2. 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("连接失败");
        //失败重连逻辑
    });