wxw
8 天以前 6a738089d6471d048c32ce7f3dcbd15c935ada79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import * as SignalR from '@microsoft/signalr';
import { getToken } from '/@/utils/axios-utils';
 
// 初始化SignalR对象
const connection = new SignalR.HubConnectionBuilder()
    .configureLogging(SignalR.LogLevel.Information)
    .withUrl(`${window.__env__.VITE_API_URL}/hubs/CheckTask?token=${getToken()}`, { transport: SignalR.HttpTransportType.WebSockets, skipNegotiation: true })
    .withAutomaticReconnect({
        nextRetryDelayInMilliseconds: () => {
            return 5000; // 每5秒重连一次
        },
    })
    .build();
 
connection.keepAliveIntervalInMilliseconds = 15 * 1000; // 心跳检测15s
connection.serverTimeoutInMilliseconds = 30 * 60 * 1000; // 超时时间30m
 
// 启动连接
connection.start().then(() => {
    console.log('启动连接checkTask');
});
// 断开连接
connection.onclose(async () => {
    console.log('断开连接checkTask');
});
// 重连中
connection.onreconnecting(() => {
    console.log('服务器已断线checkTask');
});
// 重连成功
connection.onreconnected(() => {
    console.log('重连成功checkTask');
});
// 关闭连接的方法
async function stopConnection() {
    try {
      await connection.stop();
      console.log('连接已关闭');
    } catch (error) {
      console.error('关闭连接时发生错误:', error);
    }
  }
// connection.on('PublicPlcConn', () => {});
 
export { connection as signalR,stopConnection };