Socket.js
4.2 KB
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
// //////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016-present
// All Rights Reserved.
//
// Author: AlexWang
// Date: 2016-08-27 21:40:49
// Last Modified by: AlexWang
// Last Modified time: 2017-05-20 12:38:31
// QQ Email: 1669499355@qq.com
// Description: 底层Socket管理器,保持一直在线及异常重连.
//
// //////////////////////////////////////////////////////////////////////////////
import Emiter from 'Emiter';
import Loger from 'Loger';
let loger = Loger.getLoger('EverSocket');
class EverSocket extends Emiter {
constructor() {
super();
this._connected = false;
this._lastActiveTime = 0;
this._enableEverSocket = false;
}
begin(ip, port) {
loger.log('开始WebSocket应用.');
this._enableEverSocket = true;
this.wsURL = 'ws://' + ip + ':' + port;
this._newConnection();
}
end() {
loger.log('停止WebSocket应用.');
this._clear();
}
get connected() {
return this._connected;
}
send(data) {
if (this._connected) {
loger.log('SEND MESSAGE---->', data);
this.websocket.send(data);
} else {
loger.warn('WebSocket未建立连接.消息忽略');
}
}
_setConnected(isConn = true) {
this._connected = isConn;
if (this._connected) {
this.emit(EverSocket.OPEN);
} else {
this.emit(EverSocket.CLOSED);
}
}
_newConnection() {
this.websocket = new WebSocket(this.wsURL);
this.websocket.binaryType = 'arraybuffer';
this.websocket.onopen = this._onOpen.bind(this);
this.websocket.onclose = this._onClose.bind(this);
this.websocket.onerror = this._onError.bind(this);
this.websocket.onmessage = this._onMessage.bind(this);
}
_reConnection() {
this._clear();
this.reConnectionTimeout = window.setTimeout(() => {
loger.log('WebSocket重新建立.');
window.clearTimeout(this.reConnectionTimeout);
this._newConnection();
}, EverSocket.RECONN_INTERVAL);
}
_clear() {
loger.log('WebSocket,Timers销毁');
window.clearInterval(this.pingTimer);
window.clearInterval(this.pongTimer);
this.websocket.onopen = undefined;
this.websocket.onclose = undefined;
this.websocket.onerror = undefined;
this.websocket.onmessage = undefined;
try {
this.websocket.close();
} catch (e) {
loger.log('ignore errors');
}
this.websocket = undefined;
this._enableEverSocket = false;
this._setConnected(false);
}
_onOpen() {
loger.log('WebSocket建立成功', this.wsURL);
//this.pingTimer = window.setInterval(this._sendPingHandler.bind(this), EverSocket.PING_INTERVAL);
//this.pongTimer = window.setInterval(this._checkPongHandler.bind(this), EverSocket.PONG_INTERVAL);
this._setConnected();
}
_onClose(closeEvent) {
loger.log(`WebSocket连接断开 CODE:${closeEvent.code} REASON:${closeEvent.reason} CLEAN: ${closeEvent.wasClean}`, this.wsURL);
this._reConnection();
}
_onError() {
loger.log('WebSocket错误出现');
this._connected = false;
this._reConnection();
}
_onMessage(messageEvent) {
loger.log('<----RECEIVE MESSAGE');
this._lastActiveTime = Date.now();
const bufferData = messageEvent.data;
if (bufferData.byteLength > 0) {
this.emit(EverSocket.MESSAGE, bufferData);
}
}
_sendPingHandler() {
if (this._connected) {
this.websocket.send(new ArrayBuffer);
} else {
this._reConnection();
}
}
_checkPongHandler() {
let pongTime = Date.now();
if (this._lastActiveTime &&
this._lastActiveTime >= pongTime - EverSocket.PONG_INTERVAL &&
this._lastActiveTime <= pongTime
) {} else {
loger.warn('---服务器PINGPONG超时-----');
this._reConnection();
}
}
}
EverSocket.prototype.PONG_INTERVAL = EverSocket.PONG_INTERVAL = 5000;
EverSocket.prototype.PING_INTERVAL = EverSocket.PING_INTERVAL = 3000;
EverSocket.prototype.RECONN_INTERVAL = EverSocket.RECONN_INTERVAL = 2000;
EverSocket.prototype.CONNECTING = EverSocket.CONNECTING = 0;
EverSocket.prototype.OPEN = EverSocket.OPEN = 1;
EverSocket.prototype.CLOSING = EverSocket.CLOSING = 2;
EverSocket.prototype.CLOSED = EverSocket.CLOSED = 3;
EverSocket.prototype.MESSAGE = EverSocket.MESSAGE = 4;
export default new EverSocket();