YingXingAI/uni_modules/Lyuan-SignalR
JiXinHui 631525e44e 初始化 2025-06-30 14:43:02 +08:00
..
js_sdk 初始化 2025-06-30 14:43:02 +08:00
changelog.md 初始化 2025-06-30 14:43:02 +08:00
package.json 初始化 2025-06-30 14:43:02 +08:00
readme.md 初始化 2025-06-30 14:43:02 +08:00

readme.md

Lyuan-SignalR

使用方法与官方文档一致,请自行参考官方文档

兼容微信小程序、h5、Android、ios ,其他小程序未测, 理论支持。 断线重连、重试等功能需自行判断,该库修改自微软官方。

import {
	HubConnectionBuilder,
	LogLevel
} from './signalr.js';
 const connection = new HubConnectionBuilder()
   .withUrl("http://xxx.com/messagehub", {
     // accessTokenFactory: () => {
     //     return 'token';
     // }
   })
   .configureLogging(LogLevel.Trace)
   .build();
 
 //接收消息
 connection.on("ReceiveMessage", function (user, message) {
   ;
 });
 
 //发送消息, 需在链接成功后调用
 //connection.invoke('方法名',参数)
 
 //链接断开
 connection.on/close(function () { //去掉斜杠
   ;
   //重试
   start(ms);
 });
 
 const ms = 5000;
 const start = (timer) => {
   setTimeout(() => {
     try {
       connection
         .start()
         .then(() => {
           ;
         })
         .catch((e) => {
           //重试
           // console.log("websocket连接失败", e);
           start(ms);
         });
     } catch (e) {
       //重试
       start(ms);
     }
   }, timer);
 };
 
 //开始连接
 start(0);

###觉得有用的话,可直接打赏或微信