-
Notifications
You must be signed in to change notification settings - Fork 5
/
ezy-client.js
302 lines (274 loc) · 8.9 KB
/
ezy-client.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import Const from './ezy-constants';
import Util from './ezy-util';
import Event from './ezy-events';
import Manager from './ezy-managers';
import Socket from './ezy-sockets';
import EzySetup from './ezy-setup';
import EzyEventMessageHandler from './ezy-event-message-handler';
/**
* Wrapper for JS built-in WebSocket to communicate with websocket server
*/
class EzyConnector {
constructor() {
this.ws = null;
this.destroyed = false;
this.disconnectReason = null;
}
/**
* Create connection to a websocket server
* Websocket events:
* - onerror: fire when connection to server is unsuccessful
* - onopen: fire when connection to server is successful
* - onclose: fire when connection to server is closed
* - onmessage: fire when client receives a message from server
* @param {EzyClient} client The client from which this connect function is called
* @param {string} url The websocket url
*/
connect(client, url) {
this.disconnectReason = null;
this.ws = new WebSocket(url);
let that = this;
var failed = false;
let pingManager = client.pingManager;
let eventMessageHandler = client.eventMessageHandler;
this.ws.onerror = function (e) {
Util.EzyLogger.console(
'connect to: ' + url + ' error : ' + JSON.stringify(e)
);
failed = true;
var event = new Event.EzyConnectionFailureEvent(
Const.EzyConnectionFailedReason.UNKNOWN
);
eventMessageHandler.handleEvent(event);
};
this.ws.onopen = function (e) {
Util.EzyLogger.console('connected to: ' + url);
client.reconnectCount = 0;
client.status = Const.EzyConnectionStatus.CONNECTED;
var event = new Event.EzyConnectionSuccessEvent();
eventMessageHandler.handleEvent(event);
};
this.ws.onclose = function (e) {
if (failed) return;
if (that.destroyed) return;
if (client.isConnected()) {
var reason =
that.disconnectReason || Const.EzyDisconnectReason.UNKNOWN;
eventMessageHandler.handleDisconnection(reason);
} else {
Util.EzyLogger.console(
'connection to: ' + url + ' has disconnected before'
);
}
};
this.ws.onmessage = function (event) {
if (that.destroyed) return;
pingManager.lostPingCount = 0;
var data = event.data;
var message = JSON.parse(data);
eventMessageHandler.handleMessage(message);
};
}
/**
* Close the websocket connection with a reason
* @param {string} reason
*/
disconnect(reason) {
if (this.ws) {
this.disconnectReason = reason;
this.ws.close();
}
}
/**
* Client purposely closes the websocket connection
*/
destroy() {
this.destroyed = true;
this.disconnect();
}
/**
* Send data from client to websocket server
* @param {string} data
*/
send(data) {
var json = JSON.stringify(data);
this.ws.send(json);
}
}
/**
* A proxy class that combine all needed functions into one.
* Each zone has a client, which manages the connection to server within that zone
*/
class EzyClient {
/**
* Create a client
* @param {EzyClientConfig} config Client name configuration and reconnect configuration
*/
constructor(config) {
this.config = config;
this.name = config.getClientName();
this.url = null;
this.connector = null;
this.zone = null;
this.me = null;
this.status = Const.EzyConnectionStatus.NULL;
this.reconnectCount = 0;
this.reconnectTimeout = null;
this.pingManager = new Manager.EzyPingManager();
this.pingSchedule = new Socket.EzyPingSchedule(this);
this.handlerManager = new Manager.EzyHandlerManager(this);
this.setup = new EzySetup(this.handlerManager);
this.unloggableCommands = [
Const.EzyCommand.PING,
Const.EzyCommand.PONG,
];
this.eventMessageHandler = new EzyEventMessageHandler(this);
this.pingSchedule.eventMessageHandler = this.eventMessageHandler;
}
/**
* Connect to server via a websocket url
* @param url Websocket url
*/
connect(url) {
this.url = url ? url : this.url;
this.preconnect();
this.reconnectCount = 0;
this.status = Const.EzyConnectionStatus.CONNECTING;
this.connector = new EzyConnector();
this.connector.connect(this, this.url);
}
/**
* Call to connect to server again
* @returns {boolean} - Whether or not the maxReconnectCount is reached
* - `false`: reach maxReconnectCount
* - `true`: otherwise
*/
reconnect() {
var reconnectConfig = this.config.reconnect;
var maxReconnectCount = reconnectConfig.maxReconnectCount;
if (this.reconnectCount >= maxReconnectCount) return false;
this.preconnect();
this.status = Const.EzyConnectionStatus.RECONNECTING;
this.reconnectTimeout = setTimeout(() => {
this.connector = new EzyConnector();
this.connector.connect(this, this.url);
}, reconnectConfig.reconnectPeriod);
this.reconnectCount++;
var event = new Event.EzyTryConnectEvent(this.reconnectCount);
this.eventMessageHandler.handleEvent(event);
}
/**
* Reset state before creating a new connection
*/
preconnect() {
this.zone = null;
this.me = null;
this.appsById = {};
if (this.connector) this.connector.destroy();
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
}
/**
* Call to disconnect from websocket server with a reason
* @param {string} reason Reason to disconnect
*/
disconnect(reason) {
var actualReason = reason || Const.EzyDisconnectReason.CLOSE;
this.internalDisconnect(actualReason);
}
close() {
this.disconnect();
}
internalDisconnect(reason) {
if (this.connector) this.connector.disconnect(reason);
}
/**
* Send data to websocket server
* @param data
*/
send(cmd, data) {
this.sendRequest(cmd, data);
}
/**
* Send a command and data to server
* @param {string} cmd Command to be sent
* @param {string} data Data to be sent
*/
sendRequest(cmd, data) {
if (!this.unloggableCommands.includes(cmd)) {
Util.EzyLogger.console(
'send cmd: ' + cmd.name + ', data: ' + JSON.stringify(data)
);
}
var request = [cmd.id, data];
this.connector.send(request);
}
/**
* Listen to `disconnect` event from server
* @param reason Reason that server disconnect
*/
onDisconnected(reason) {
this.status = Const.EzyConnectionStatus.DISCONNECTED;
this.pingSchedule.stop();
this.internalDisconnect();
}
/**
* Check connection status
* @returns {boolean} Whether or not the status is CONNECTED
*/
isConnected() {
var connected = this.status === Const.EzyConnectionStatus.CONNECTED;
return connected;
}
/**
* Get first app from this client zone
* @returns {EzyApp} Queried app
*/
getApp() {
if (!this.zone) return null;
var appManager = this.zone.appManager;
return appManager.getApp();
}
/**
* Get app from this client zone by id
* @param appId id of queried app
* @returns {EzyApp} Queried app
*/
getAppById(appId) {
if (!this.zone) return null;
var appManager = this.zone.appManager;
return appManager.getAppById(appId);
}
/**
* Get plugin from this client zone by id
* @param pluginId id of queried plugin
* @returns {EzyPlugin} Queried plugin
*/
getPluginById(pluginId) {
if (!this.zone) return null;
var pluginManager = this.zone.pluginManager;
return pluginManager.getPluginById(pluginId);
}
newAppManager(zoneName) {
return new Manager.EzyAppManager(zoneName);
}
/**
* Get the app manager of this client zone
* @returns {EzyAppManager} App manager of current client zone
*/
getAppManager() {
if (!this.zone) return null;
return this.zone.appManager;
}
newPluginManager(zoneName) {
return new Manager.EzyPluginManager(zoneName);
}
/**
* Get the plugin manager of this client zone
* @returns {EzyPluginManager} Plugin manager of current client zone
*/
getPluginManager() {
if (!this.zone) return null;
return this.zone.pluginManager;
}
}
export default EzyClient;