-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathezy-event-message-handler.js
44 lines (38 loc) · 1.63 KB
/
ezy-event-message-handler.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
var EzyEventMessageHandler = function (client) {
this.client = client;
this.handlerManager = client.handlerManager;
this.unloggableCommands = client.unloggableCommands;
this.handleEvent = function (event) {
var eventHandler = this.handlerManager.getEventHandler(event.getType());
if (eventHandler) eventHandler.handle(event);
else EzyLogger.console('has no handler with event: ' + event.getType());
};
this.handleDisconnection = function (reason) {
this.client.onDisconnected(reason);
var event = new EzyDisconnectionEvent(reason);
this.handleEvent(event);
};
this.handleMessage = function (message) {
var cmd = EzyCommands[message[0]];
var data = message.length > 1 ? message[1] : [];
if (!this.unloggableCommands.includes(cmd))
EzyLogger.console(
'received cmd: ' + cmd.name + ', data: ' + JSON.stringify(data)
);
if (cmd === EzyCommand.DISCONNECT) this.handleDisconnectionData(data);
else this.handleResponseData(cmd, data);
};
this.handleStreaming = function (bytes) {
var streamingHandler = this.handlerManager.streamingHandler;
streamingHandler.handle(bytes);
};
this.handleDisconnectionData = function (resonseData) {
var reason = resonseData[0];
this.handleDisconnection(reason);
};
this.handleResponseData = function (cmd, responseData) {
var handler = this.handlerManager.getDataHandler(cmd);
if (handler) handler.handle(responseData);
else EzyLogger.console('has no handler with command: ' + cmd.name);
};
};