wang
2024-03-28 04adb88a2ed54cdf4c2958c79972c30109b9b5b6
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var websock = null;
var global_callback = null;
let isConnect = false; //连接标识 避免重复连接
let rec; //断线重连后,延迟5秒重新创建WebSocket连接  rec用来存储延迟请求的代码
let soketparams ='心跳包检测'
 
 
var serverPort = "/ws"; // webSocket连接端口
var wsuri = "ws://localhost:8081/mesModuleTools";
 
function createWebSocket(callback) {
 
    if (websock == null || typeof websock !== WebSocket) {
        initWebSocket(callback);
    }
}
 
function global_callback1(msg) {
    console.log("websocket的回调函数收到服务器信息:" + JSON.stringify(msg));
    // console.log("收到服务器信息:" + msg);
}
 
function initWebSocket(callback) {
    global_callback = callback;
    // 初始化websocket
    websock = new WebSocket(wsuri);
    websock.onmessage = function (e) {
        websocketonmessage(e);
    };
    websock.onclose = function (e) {
        websocketclose(e);
    };
    websock.onopen = function () {
        websocketOpen();
    };
 
    // 连接发生错误的回调方法
    websock.onerror = function () {
        console.log("WebSocket连接发生错误");
    };
}
 
 
 
//心跳设置
var heartCheck = {
    timeout: 20000, //每段时间发送一次心跳包 这里设置为20s
    timeoutObj: null, //延时发送消息对象(启动心跳新建这个对象,收到消息后重置对象)
 
    start: function () {
        this.timeoutObj = setInterval(function () {
            if (isConnect) websock.send(JSON.stringify(soketparams));
        }, this.timeout);
    },
 
    reset: function () {
        clearTimeout(this.timeoutObj);
        this.start();
    }
};
 
//定义重连函数
let reConnect = () => {
    console.log("尝试重新连接");
    if (isConnect) return; //如果已经连上就不在重连了
    rec && clearTimeout(rec);
    rec = setTimeout(function () { // 延迟5秒重连  避免过多次过频繁请求重连
        initWebSocket();
    }, 5000);
};
 
 
 
// 实际调用的方法
function sendSock(agentData ) {
    if (websock.readyState === websock.OPEN) {
        // 若是ws开启状态
        websocketsend(agentData);
    } else if (websock.readyState === websock.CONNECTING) {
        // 若是 正在开启状态,则等待1s后重新调用
        setTimeout(function () {
            sendSock(agentData);
        }, 1000);
    } else {
        // 若未开启 ,则等待1s后重新调用
        setTimeout(function () {
            sendSock(agentData);
        }, 1000);
    }
}
 
function closeSock() {
    console.log('关闭了')
    websock.close();
}
 
// 数据接收
function websocketonmessage(msg) {
    // console.log("收到数据:"+JSON.parse(e.data));
    // console.log("收到数据:"+msg);
 
    // global_callback(JSON.parse(msg.data));
    // 收到信息为Blob类型时
    let result = null;
    if (msg.data instanceof Blob) {
        const reader = new FileReader();
        reader.readAsText(msg.data, "UTF-8");
        reader.onload = (e) => {
            result = JSON.parse(reader.result);
            //console.log("websocket收到", result);
            global_callback(result);
        };
    } else {
        result = JSON.parse(msg.data);
        //console.log("websocket收到", result);
        global_callback(result);
    }
}
 
// 数据发送
function websocketsend(agentData) {
    // console.log("发送数据:" + agentData);
    websock.send(agentData);
}
 
// 关闭
function websocketclose(e) {
    console.log("connection closed (" + e.code + ")");
}
 
function websocketOpen(e) {
    console.log("连接打开");
    isConnect = true
    //heartCheck.start(); //发送心跳 看个人项目需求
}
 
export { sendSock, createWebSocket, closeSock ,global_callback1};