DELL_jzs
2022-02-26 c25482e9cc5aa1343d7b38bff3d06d0d9c86c1c7
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * 服务地址
 */
let url = "ws://127.0.0.1:5050/";
/**
 * WebSocketClient 客户端
 */
let WebSocketClient;
 
/**
 * 客户端状态
 */
let Client = {
    /**是否连接 */
    IsOpen: false,
    /**最新收到的服务端信息 */
    Receive: null,
    /**重连检测计时器 */
    JiShiQi: null,
    /**打开事件 */
    OnOpen: null,
    /**关闭事件 */
    OnClose: null,
    /**接收到信息事件 */
    OnMsg: null,
}
 
/**
 * 信息返回类型
 */
let Result = {
    Order: "",
    Time: new Date()
};
 
/**
 * 打开连接完成
 * @param {*} model 
 */
function OnOpen(model) {
    console.log("打开连接完成");
    Client.IsOpen = true;
 
    if (typeof (Client.OnOpen) == 'function') {
        Client.OnOpen();
    }
}
 
/**
 * 连接断开
 * @param {*} model 
 */
function OnClose(model) {
    // console.log("连接断开");
    Client.IsOpen = false;
 
    if (typeof (Client.OnClose) == 'function') {
        Client.OnClose();
    }
}
 
/**
 * 错误
 * @param {*} error 
 */
function OnError(error) {
    //console.error(error);
}
 
/**
 * 客户端主动向服务端发送消息
 * @param {String} msg 发送的字符串
 */
function ClientSendToServerMsg(msg) {
    if (msg != null && msg.length > 0) {
        WebSocketClient.send(msg);
    }
}
 
/**
 * 客户端主动向服务端发送实例
 * @param {Object} obj 对象实例
 */
function ClientSendToServerObj(obj) {
    if (null != obj) {
        var msg = JSON.stringify(obj);
        WebSocketClient.send(msg);
    }
}
 
/**
 * 数据接收
 * @param {*} model 
 */
function OnMessage(model) {
 
    var result = JSON.parse(model.data);
    console.log(result);
 
    Client.Receive = result;
    // Result.Order = result.Order;
    // Result.Time = new Date();
 
    // ClientSendToServerObj(Result);
 
    if (typeof (Client.OnMsg) == 'function') {
        Client.OnMsg(result);
    }
}
 
/**
 * 打开连接
 */
function Open() {
 
    //隔离执行,连接失败后可继续连接
    (function () {
 
        //初始化参数
        Client.IsOpen = false;
        Client.Receive = null;
 
        Result.Order = "";
        Result.Time = new Date();
 
        if ('WebSocket' in window) {
            WebSocketClient = new WebSocket(url);
        }
        else if ('MozWebSocket' in window) {
            WebSocketClient = new MozWebSocket(url);
        }
        else {
            alert("浏览器不支持WebSocket");
        }
 
        WebSocketClient.onopen = OnOpen;
        WebSocketClient.onclose = OnClose;
        WebSocketClient.onerror = OnError;
        WebSocketClient.onmessage = OnMessage;
    })();
}
 
/**
 * 检测状态
 */
function Detect() {
    //已连接
    if (Client.IsOpen && WebSocketClient.readyState == 1) {
        //Result.Time = new Date();
        //ClientSendToServerObj(Result);
    } else {
        Open();
    }
    // console.log(Client);
    // console.log(WebSocketClient);
}
 
 
//启动
(function () {
    Open();
    Client.JiShiQi = setInterval(Detect, 5000);
})()