标签:ref var c99 timeout 参考 ted cti creat 时间
1 var connection = new signalR.HubConnectionBuilder() 2 .withUrl("http://localhost:5000/doopstream") 3 //重连函数,参数是重连间隔时间,单位毫秒 4 .withAutomaticReconnect([0, 3000, 5000, 10000, 15000, 30000]) 5 .configureLogging(signalR.LogLevel.Information) 6 .build();
1 //启动模块 2 async function start(){ 3 connection.start().then(()=>getdata()).catch(err => { 4 var msg="strat:"+err.toString(); 5 console.error(msg); 6 setTimeout(()=>start(),5000); 7 }); 8 } 9 //触发启动 10 start();
1 //重连成功后的处理机制 2 connection.onreconnected((connectionId)=>{ 3 4 console.log("onreconnected:"+connectionId); 5 getdata(); 6 });
1 //连接关闭后的处理机制 2 connection.onclose(async (error)=>{ 3 var msg="close:"+error.toString(); 4 console.error(msg); 5 await start(); 6 });
1 /接收数据逻辑 2 function getdata() { 3 console.log("connected"); 4 connection.invoke("Subscribe", ‘test‘).catch(err => console.error(err.toString())); 5 connection.on("Receive", function (ch, message) { 6 console.log(message); 7 var p = document.createElement("p"); 8 p.innerHTML = ch + ‘#‘ + message; 9 var div = document.getElementById("div"); 10 div.appendChild(p); 11 }); 12 };
PS:SignalR js 库需要用 @microsoft/signalr@3.1.2 库,不然没有 withAutomaticReconnect 这个函数
https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-3.1
PS:不知道是我的理解能力太差了,还是微软的中文文档没写好,折腾了一小会搞懂了,记录一下
标签:ref var c99 timeout 参考 ted cti creat 时间
原文地址:https://www.cnblogs.com/ganqiyin/p/12518473.html