-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketClientManager.js
79 lines (65 loc) · 2.15 KB
/
SocketClientManager.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
var EventEmitter = require("events").EventEmitter;
var canvasManager = require("./managers").getCanvasManager();
var SocketClient = function (id, socket) {
EventEmitter.call(this);
this.id = id;
this.socket = socket;
// setup socket event handling
socket.on("message", this.onMessage.bind(this));
socket.once("close", this.onClose.bind(this));
socket.on("error", this.onError.bind(this));
};
SocketClient.prototype = Object.create(EventEmitter.prototype);
SocketClient.prototype.constructor = SocketClient;
SocketClient.prototype.onMessage = function (data, flags) {
if (flags && flags.binary) {
console.error("invalid message: binary not supported");
// binary messages are not supported
this.socket.close(1003, "invalid message: binary not supported")
return;
}
var message;
try {
message = JSON.parse(data);
} catch (e) {
console.error("invalid message: parse failure\n", e);
// disconnect
this.socket.close(1008, "invalid message: parse failure");
return;
}
this.handleMessage(message).catch(function (e) {
console.error("error handling message:\n", e);
});
};
SocketClient.prototype.onClose = function () {
this.emit("disconnect");
};
SocketClient.prototype.onError = function (err) {
console.error(err);
};
SocketClient.prototype.handleMessage = function (message) {
switch(message.t) {
case "s":
// handle stroke
return canvasManager.handleStroke(message.d).then(function () {
// if the stroke assertion passes, share the stroke
// with other socket clients at this point...
});
default:
// unknown message type
break;
}
};
var SocketClientManager = module.exports = function () {
this.idCounter = 1;
this.clients = {};
};
SocketClientManager.prototype.handleConnection = function (socket) {
var self = this;
var id = this.idCounter++;
var client = new SocketClient(id, socket);
this.clients[id] = client;
client.once("disconnect", function () {
delete self.clients[id];
});
};