-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
299 lines (256 loc) · 8.54 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Module Dependencies
*/
var EventEmitter = require('events').EventEmitter;
var Util = require('util');
var _ = require('lodash');
/**
* @constructor
*/
function Connection() {
/**
* Define if the connection needs to be authenticated by the client
* If this is not the case, the connection ID will be used instead
*
* @type {boolean}
*/
this.require_authentication = false;
this.bundling = false;
this.logging = function(){};
this._connections = {};
this._pending_connections = {};
this._sockjs = null;
this._clientCallbacks = {};
this.on('connect', this.onConnect.bind(this));
this.on('close', this.onClose.bind(this));
}
/**
* Add event module
*/
Util.inherits(Connection, EventEmitter);
/**
* Start the connection
*
* @param {SockJS} sockjs
* @param {object} options
*/
Connection.prototype.start = function (sockjs, options) {
this._sockjs = sockjs;
var self = this;
// Parse options
options = options || {};
this.require_authentication = (options.hasOwnProperty('authentication')) ? options.authentication : this.require_authentication;
this.logging = (options.hasOwnProperty('logging')) ? options.logging : this.logging;
this.bundling = (options.hasOwnProperty('bundling')) ? options.bundling : this.bundling;
this.logging.call(null, 'Connection :: Starting socket listeners');
// Sockjs Events
sockjs.on('connection', function (client) {
// Add to connections
if(self.require_authentication) {
self._pending_connections[client.id] = client;
} else {
self._connections[client.id] = client;
}
self.emit('connect', client);
// Handle connection close event
client.on('close', function () {
self.emit('close', client);
});
// Handle incoming data events
client.on('data', function (data) {
// Parse message
try {
var message = JSON.parse(data);
} catch (e) {
self.logging.call(null, "Connection :: Error :: Failed to parse JSON :: ", e);
return;
}
// Check for type
if (!message.hasOwnProperty('type')) {
self.logging.call(null, "Connection :: Error :: No message type specified");
return;
}
// If the connection is trying to authenticate add authentication function
if ('authenticate' == message.type) {
message.data.authenticate = function (user) {
return self.authenticate(client.id, user);
};
self.emit(message.type, message.data, function(data) {
client.write(JSON.stringify({
type: 'callback',
data: data,
callback_id: message.callback_id
}));
});
return;
}
// If the connection is not authenticated return an error
if (!client.hasOwnProperty('user_id')) {
client.write(JSON.stringify({
type: message.type,
data: {type: 'error', message: 'Not authenticated', timestamp: message.data.timestamp}
}));
return;
}
// Or add the authenticated user_id to the message
else {
message.data.user_id = client.user_id;
if(self._clientCallbacks.hasOwnProperty(client.user_id)) {
_.forEach(self._clientCallbacks[client.user_id], function(callback) {
callback(message);
});
}
}
// If the client has included a callback_id, prepare the callback function and emit
if (message.hasOwnProperty('callback_id')) {
var callback = message.callback_id;
if(!self.bundling) {
callback = function(data) {
client.write(JSON.stringify({
type: 'callback',
data: data,
callback_id: message.callback_id
}));
};
}
self.emit(message.type, message.data, callback);
}
// Emit and catch callbacks that shouldn't be called
else {
self.emit(message.type, message.data, function () {
self.logging.call(null, "Connection :: Warning :: Client did not specify callback function");
});
}
});
});
};
/**
* Authenticate a given connection_id with a user
*
* The connection will be deleted from _pending_connections and added to _connections
* And a user_id will be added to the connection object
*
* @param {any} connection_id
* @param {object} user | Needs property id
* @returns {boolean}
*/
Connection.prototype.authenticate = function (connection_id, user) {
this.logging.call(null, 'Connection :: Authenticate');
if (this._pending_connections.hasOwnProperty(connection_id)) {
var id = (user.hasOwnProperty('id')) ? user.id : user.get('id');
this._connections[id] = this._pending_connections[connection_id];
this._connections[id].user_id = id;
user.getConnection = function() {
return this._connections[id];
}.bind(this);
user.send = function(event, data) {
return this.send(event, data, id);
}.bind(this);
delete this._pending_connections[connection_id];
this.emit('authenticated', user);
return true;
}
this.logging.call(null, "Connection :: Authenticate :: Failed to find the connection");
return false;
};
/**
* If there is no authentication required, add the connection ID as user_id
*
* @param {object} client
*/
Connection.prototype.onConnect = function(client) {
if (!this.require_authentication && !client.hasOwnProperty('user_id')) {
client.user_id = client.id;
}
};
/**
* If the client disconnects, remove it from the connection list
*
* @param {object} client
*/
Connection.prototype.onClose = function(client) {
if (client.hasOwnProperty('user_id')) {
delete this._connections[client.user_id];
} else {
delete this._pending_connections[client.id];
}
};
/**
* Allow for bundled messages with multiple callback_ids
*
* Bundle: [{callback_id: <id>, type: <type>, data: <data>}]
* Type is optional if you supply a callback_id,
* callback_id is optional if you supply a type
*
* @param {Array} bundle
* @param {int} id
*/
Connection.prototype.bundle = function(bundle, id) {
this.send('bundle', bundle, id);
};
/**
* Return the original sockjs connection object by user_id
* @param user_id
* @returns {object|null}
*/
Connection.prototype.getConnection = function(user_id) {
return (this._connections.hasOwnProperty(user_id)) ? this._connections[user_id] : null;
};
/**
* Send data to a specific user
*
* @param {string} type
* @param {object} data
* @param {integer} id
* @returns {boolean}
*/
Connection.prototype.send = function (type, data, id) {
var _data = {type: type, data: data};
if (this._connections.hasOwnProperty(id)) {
this._connections[id].write(JSON.stringify(_data));
return true;
}
return false;
};
/**
* Send data to an array of IDs using the Connection.send function
*
* @param {string} type
* @param {object} data
* @param {array} list
*/
Connection.prototype.broadcastTo = function (type, data, list) {
var self = this;
_.forEach(list, function(id) {
if (self._connections.hasOwnProperty(id)) {
self.send(type, data, id);
}
});
};
/**
* Send data to all users using the Connection.send function
*
* @param {string} type
* @param {object} data
*/
Connection.prototype.broadcast = function (type, data) {
var self = this;
_.forEach(self._connections, function(id){
self.send(type, data, id);
});
};
/**
* Allow for registering callbacks directly onto a user_id
*/
Connection.prototype.addClientCallback = function(client_id, callback) {
if(!this._clientCallbacks.hasOwnProperty(client_id)) {
this._clientCallbacks[client_id] = [];
}
this._clientCallbacks[client_id].push(callback);
};
/**
* Module Exports
*
* @type {Connection}
*/
module.exports = exports = Connection;