-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.js
48 lines (40 loc) · 1.06 KB
/
realtime.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
'use strict';
var socket = require('socket.io');
var io;
function realtime (server) {
if (io) { return io; }
io = realtime.io = addExceptMethod(socket(server));
io.on('connection', connected);
return io;
function connected (socket) {
socket.on('error', console.error.bind(console));
socket.on('/skyrocket/join', join);
socket.on('/skyrocket/leave', leave);
function join (data) {
data.rooms.forEach(socket.join, socket);
}
function leave (data) {
data.rooms.forEach(socket.leave, socket);
}
}
}
function addExceptMethod (io) {
var sockets = io.sockets;
var _broadcast = sockets.adapter.broadcast;
sockets.constructor.prototype.except = except;
sockets.adapter.broadcast = broadcast;
return io;
function except (id) {
this.excepts = this.excepts || [];
this.excepts.push(id);
return this;
}
function broadcast (packet, options) {
if (sockets.excepts) {
options.except = sockets.excepts;
}
_broadcast.apply(this, arguments);
delete sockets.excepts;
}
}
module.exports = realtime;