虎绿林WebSocket消息推送服务文档及参考客户端

@Ta 2023-03-19发布,2023-05-03修改 14478点击

推送服务器源代码:

https://github.com/rkonfj/hu60bot
https://github.com/hu60t/hu60bot

@rkonfj 创建。

通过nginx转发websocket的参考配置

    location /ws/msg
    {
        proxy_pass http://127.0.0.1:4860/v1/ws;

        # proxy websocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;

        proxy_read_timeout 600s;
        proxy_send_timeout 600s;
    }

WebSocket推送服务器地址:

wss://hu60.cn/ws/msg

认证方式:

  1. Cookie hu60_sid(由浏览器自动发送,不支持跨域)
Cookie: hu60_sid=用户sid
  1. GET参数_sid(支持跨域)
wss://hu60.cn/ws/msg?_sid=用户sid

参考客户端:

function connectToWebSocket() {
  const socket = new WebSocket("wss://" + location.host + "/ws/msg");
  let keepAliveTimer = null;

  // 开启 WebSocket 连接时触发
  socket.onopen = (event) => {
    console.log("WebSocket 连接已经建立");

    // 连上推送服务器后还要再查询一次消息数量接口,防止错过还没连上的这段时间发来的消息
    fetch('/q.php/user.stat.json').then(async function(result) {
        let json = await result.json();
        console.log('新内信条数:', json.newMsg, '新@消息条数', json.newAtInfo);
    });

    // 每隔一分钟发送一个 keep alive 消息,防止连接断开
    keepAliveTimer = setInterval(() => {
      socket.send('{"action":"ping"}');
    }, 60000);
  }

  // 接收到 WebSocket 消息时触发
  socket.onmessage = (event) => {
    console.log("收到 WebSocket 消息", event.data);
  };

  // 当 WebSocket 连接出错时触发
  socket.onerror = (event) => {
    console.error("WebSocket 连接出错", event);
    // 关闭当前 WebSocket 连接
    socket.close();

    // 注意:不要在此处重连 WebSocket,否则一次连接失败会触发两次重连,连接数会指数增长
    // 确保只在 onclose 中进行重连。
  };

  // 当 WebSocket 连接关闭时触发
  socket.onclose = (event) => {
    // 取消 keep alive 定时器
    clearInterval(keepAliveTimer);

    console.log("WebSocket 连接已关闭", event);

    // 重新连接 WebSocket
    setTimeout(() => {
      console.log("重新连接 WebSocket");
      connectToWebSocket();
    }, 5000); // 延迟 5 秒重新连接
  };
}

// 连接 WebSocket
connectToWebSocket();

收到的推送消息event.data可能会是以下几种格式:

  1. 刚连上时收到的消息。
{"event": "connected"}
  1. keep-alive响应,只是为了保持连接活跃。
{"event":"ping","data":"pong"}
  1. 内信推送,type字段为0
{"event":"msg","data":{"id":995610,"touid":1,"byuid":1,"type":0,"isread":0,"content":"[{\"type\":\"markdown\"},{\"type\":\"text\",\"value\":\"\\r\\n\"},{\"type\":\"face\",\"face\":\"滑稽\"}]","ctime":"0001-01-01T00:00:00Z"}}
  1. @消息推送,type字段为1。
{"event":"msg","data":{"id":995612,"touid":1,"byuid":1,"type":1,"isread":0,"content":"[{\"type\":\"atMsg\",\"uid\":1,\"pos\":\"聊天室“秘密虎穴”第688楼中\",\"url\":\"addin.chat.%E7%A7%98%E5%AF%86%E8%99%8E%E7%A9%B4.{$BID}?floor=688#688\",\"msg\":[{\"type\":\"markdown\"},{\"type\":\"text\",\"value\":\"\\r\\n\"},{\"type\":\"at\",\"tag\":\"老虎会游泳\",\"uid\":1},{\"type\":\"text\",\"value\":\",\"},{\"type\":\"face\",\"face\":\"哈哈\"}]}]","ctime":"0001-01-01T00:00:00Z"}}
  1. 机器人上下线通知
{"event":"online","data":{"uid":-50,"count":1}}

取消事件订阅

可取消订阅的事件有msgonlineoffline

不需要订阅机器人上下线事件可以在 websocket 连接建立成功后发送:

{"action": "unsub", "data": ["online", "offline"]}

后续有需要这些事件再发送:

{"action": "sub", "data": ["online", "offline"]}

查询取消订阅的事件:

{"action": "lsunsub"}

查询机器人在线列表

{"action": "lsol"}

响应格式如下:

{"event":"lsol","data":{"-50":1,"-51":1,"-56":1}}
回复列表(9|隐藏机器人聊天)
添加新回复
回复需要登录