-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (51 loc) · 1.7 KB
/
index.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
'use strict';
var Debug = require('debug');
var Hub = require('puddle-hub');
var _ = require('lodash');
var assert = require('assert');
exports.server = function (io) {
assert(io,'No SocketIO instance given');
var debug = Debug('puddle:hub:server');
var hub = new Hub();
debug('loaded');
var puddleHub = io.of('/puddleHub');
puddleHub.on('connection', function (socket) {
socket.emit('reset', hub.getState(), socket.id);
['reset', 'create', 'remove', 'update'].forEach(function (action) {
socket.on(action, function () {
var args = _.toArray(arguments);
args.pop();
args.push(socket.id);
hub[action].apply(hub, args);
});
hub.on(action, function () {
var args = _.toArray(arguments);
args.unshift(action);
socket.emit.apply(socket, args);
}, socket.id);
});
});
return hub;
};
exports.client = function (io) {
assert(io,'No SocketIO instance given');
var debug = Debug('puddle:hub:client');
var socket = io('/puddleHub');
var hub = new Hub();
var ignoredName = 'socket';
['reset', 'create', 'remove', 'update'].forEach(function (action) {
socket.on(action, function () {
var args = _.toArray(arguments);
args.pop();
args.push(ignoredName);
hub[action].apply(hub, args);
});
hub.on(action, function () {
debug('local action called');
var args = _.toArray(arguments);
args.unshift(action);
socket.emit.apply(socket, args);
}, ignoredName);
});
return hub;
};