-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrosocket-server.js
88 lines (87 loc) · 2.62 KB
/
microsocket-server.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
var uid = function () {
var d = new Date().getTime();
var uuid = 'xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
};
module.exports = function (app, options) {
options = options || {};
options.url = options.url || '/socket';
var handlers = {};
var onAuth = null;
var sockets = {};
this.on = function (msg, cb) {
handlers[msg] = cb;
};
this.auth = function (cb) {
onAuth = cb;
};
var doAuth = function (req, resp) {
var authObj = req.query.body;
var authCB = onAuth || function (obj, cb) {
cb(true);
};
authCB(authObj, function (res) {
if (!res) {
resp.status(400);
resp.end();
return;
}
var sid = uid();
resp.send(sid);
var stack = [];
sockets[sid] = {stack: stack, opened: null, handlers: {}};
resp.end();
var onConnected = handlers['connection'];
if (onConnected) {
onConnected({
emit: function (msg, data) {
sockets[sid].stack.push({name: msg, obj: data});
if (sockets[sid].opened) {
sockets[sid].opened.end();
sockets[sid].opened = null;
}
},
on: function (msg, cb) {
sockets[sid].handlers[msg] = cb;
}
});
}
});
};
app.post(options.url, function (req, resp) {
var sid = req.query.sid;
if (!sid) {
doAuth(req, resp);
return;
}
var sidEntry = sockets[sid];
if (!sidEntry) {
resp.status(401);
resp.end();
return;
}
if (sidEntry.opened) {
sidEntry.opened.end();
sidEntry.opened = null;
}
for (var k in req.body) {
var obj = req.body[k];
if (obj.name && sidEntry.handlers[obj.name]) {
sidEntry.handlers[obj.name](obj.obj);
}
}
if (sidEntry.stack.length) {
console.log("sending " + JSON.stringify(sidEntry.stack));
resp.send(JSON.stringify(sidEntry.stack));
resp.end();
sidEntry.stack = [];
} else {
sidEntry.opened = resp;
}
});
return this;
};