-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrueSocial.js
578 lines (497 loc) · 14.5 KB
/
TrueSocial.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
Dependencies:
1) Signals
Message:
1) key - the event to be called
2) data - information
3) ack - acknowledgement
Peer connection config
1) Max connections
2) Connection method
*/
var TrueSocial = (function(){
function TrueSocial(o){
this.listeners = {};
// Handling options
this.options = {
signalingAddress: 'http://10.0.0.9:3000',
wwwRoot: 'root',
category: 'test',
port: 8080,
isServer: ( 'cordova' in window && cordova.plugins && cordova.plugins.CorHttpd ) ? true : false,
};
if(o === undefined){
o = {};
}
for(var key in o){
this.options[key] = o[key];
}
// Initializing variables
this.status = TrueSocial.STATUS_INIT;
this.available = [];
this.websockets = [];
// Testing dependencies
// Settings server callbacks
if(this.options.isServer){
httpd = cordova.plugins.CorHttpd;
httpd.socket.connected = onServerConnected.bind(this);
}
// Connecting to signaling server
if(!("io" in window)){
this.addSocketScript();
return;
}
};
TrueSocial.TYPE_STAGE = "stage";
TrueSocial.TYPE_PEER = "player";
TrueSocial.WEBSOCKET_SERVER = "server";
TrueSocial.WEBSOCKET_CLIENT = "client";
// Not connected to service statuses
TrueSocial.STATUS_DISCONNECTED = -1;
TrueSocial.STATUS_INIT = 0;
// X > Connecting = connected to signaling service
// X < Connecting = not connected to signaling service
TrueSocial.STATUS_CONNECTING = 1;
// Connected to service statuses
TrueSocial.STATUS_CONNECTED = 2;
TrueSocial.STATUS_DETECT_STAGE = 4;
TrueSocial.STATUS_DETECT_PEER = 5;
/*
Signaling things
*/
TrueSocial.prototype.addSocketScript = function() {
// Adds socket script to header
};
TrueSocial.prototype.init = function() {
// Connecting to signaling server
this.status = TrueSocial.STATUS_INIT;
this.signalingService = new io(this.options.signalingAddress, {
'reconnection limit': 8000,
autoConnect: false,
});
// Binding connectivity events
this.signalingService.on('connect', onConnectedSignaling.bind(this));
this.signalingService.on('disconnect', onDisconnectSignaling.bind(this));
this.signalingService.on('error', onFailSignaling.bind(this));
this.signalingService.on('connectError', onFailSignaling.bind(this));
this.signalingService.on('connectTimeout', onFailSignaling.bind(this));
this.signalingService.on('reconnect_failed', onFailSignaling.bind(this));
// Binding connectivity events
this.signalingService.on('signal:identification', onIdentification.bind(this));
this.signalingService.on('signal:unidentification', onUndentification.bind(this));
this.signalingService.on('signal:joinRequest', onSignalingJoinRequest.bind(this));
this.signalingService.on('signal:callTo', onSignalingCallTo.bind(this));
this.signalingService.on('signal:callFailed', onSignalingCallFailed.bind(this));
// Bind server WIFI
if(!this.options.isServer){
return;
}
document.addEventListener('offline', onWifiOffline.bind(this));
document.addEventListener('online', function(){
// Do something else with that
if(navigator.connection.type === Connection.WIFI){
onWifiOnline.call(this);
} else {
onWifiOffline.call(this);
}
});
};
TrueSocial.prototype.connectSignaling = function() {
// if(this.status === TrueSocial.STATUS_INIT){
// this.status = TrueSocial.STATUS_CONNECTING;
// this.signalingService.connect();
// return;
// }
// Checking status
if(this.status < TrueSocial.STATUS_CONNECTING){
// perform reconnection
this.status = TrueSocial.STATUS_CONNECTING;
this.signalingService.connect();
} else {
// stop operation and retry after X ms
this.stopSignaling();
setTimeout(function(){
this.connectSignaling();
}.bind(this), 1000)
}
};
TrueSocial.prototype.stopSignaling = function() {
// Stops all current connections and the connection to the signaling service
this.status = TrueSocial.STATUS_DISCONNECTED;
this.signalingService.disconnect();
// Check if connected
if(this.status > TrueSocial.STATUS_CONNECTING){
this.disconnect();
}
};
TrueSocial.prototype.joinResponse = function(data) {
// Overwrite me!
console.log('joinResponse: Automatic response');
return {confirm: true, reason: 'Automatic'};
};
TrueSocial.prototype.join = function(id, success, fail, done) {
// Data must contain id
this.signalingService.emit('signal:join', { id: id }, function(res){
console.log('join response', res);
if(res.confirm){
if(success){
success(res);
}
} else {
if(fail){
fail(res);
}
}
if(done){
done(res);
}
});
};
TrueSocial.prototype.updateSignaling = function(data, ack) {
this.signalingService.emit('signal:update', data, ack);
};
TrueSocial.prototype.getWebsocket = function(id) {
if(id){
return this.websockets[id];
}
for(var key in this.websockets){
return this.websockets[key];
}
return null;
};
TrueSocial.prototype.emit = TrueSocial.prototype.send = function(id, key, data, ack) {
if(id){
if(this.websockets[id]){
this.websockets[id].send(key, data, ack);
return true;
} else {
console.error('No Socket', id);
return false;
}
}
for(var id in this.websockets){
var socket = this.websockets[id];
socket.send(key, data, ack);
return true;
}
console.error('No Socket', '[Any]');
return false;
};
TrueSocial.prototype.broadcast = function(key, data) {
for(var id in this.websockets){
var socket = this.websockets[id];
socket.send(key, data);
}
};
TrueSocial.prototype.disconnectAll = function() {
var keys = [];
for(var key in this.websockets){
keys.push(key);
}
for (var i = 0; i < keys.length; i++) {
this.websockets[keys[i]].disconnect();
};
};
function onConnectedSignaling(data){
this.dispatch('connectedSignaling');
this.status = TrueSocial.STATUS_CONNECTED;
// Setting server or non-server
if(!this.options.isServer){
console.log('Does not have httpd, identified as client peer');
this.signalingService.emit('signal:identification', { category: this.options.category, data: this.options.data, isServer: this.options.isServer }, onIdentificationResponse.bind(this));
return;
}
// Set WIFI connection
if(navigator.connection.type === Connection.WIFI){
// startServer.call(this);
}
startServer.call(this);
}
function shutDownServices(){
// stop the server
if(this.options.isServer){
stopServer.call(this);
}
// stop the websockets
for (var i = 0; i < this.websockets.length; i++) {
try{
this.websockets[i].close();
}catch(err){
console.log('onDisconnectSignaling: Websocket close -> ' + err);
}
};
this.available = [];
this.websockets = [];
}
function onDisconnectSignaling(){
shutDownServices.call(this);
this.dispatch('disconnectedSignaling');
this.status = TrueSocial.STATUS_DISCONNECTED;
}
function onFailSignaling(){
this.dispatch('failSignaling');
shutDownServices.call(this);
this.status = TrueSocial.STATUS_DISCONNECTED;
}
function onWifiOffline(){
shutDownServices.call(this);
this.status = TrueSocial.STATUS_DISCONNECTED;
}
function onWifiOnline(){
}
function onIdentificationResponse(data){
this.id = data.id;
onIdentification.call(this, data.identifications);
}
function onIdentification(data){
for (var i = 0; i < this.available.length; i++) {
var peer = this.available[i];
if(peer.id === data.id){
this.available.splice(i, 1);
break;
}
};
this.available = this.available.concat(data);
this.dispatch('identification', data);
}
function onUndentification(data){
for (var i = 0; i < this.available.length; i++) {
if(this.available[i].id === data.id){
this.available.splice(i,1);
this.dispatch('unidentification', data);
break;
}
};
}
function onSignalingJoinRequest(data, ack){
this.dispatch('joinRequest', data);
var res = this.joinResponse(data);
ack(res);
}
function onSignalingCallTo(data, ack){
console.log('onSignalingCallTo', data);
var socket = new WebSocket(data.localAddress + '/service');
var wrapper = makeSocketWrapper.call(this, socket, TrueSocial.WEBSOCKET_CLIENT, data.id);
wrapper.connectAck = ack;
socket.onerror = websocketError.bind(wrapper);
socket.onopen = websocketOpen.bind(wrapper);
socket.onmessage = websocketMessage.bind(wrapper);
socket.onclose = handleClose.bind(wrapper);
this.websockets[data.id] = wrapper;
}
function onSignalingCallFailed(data){
console.log('onSignalingCallFailed', data);
this.dispatch('disconnect', data);
}
/*
Client Websocket
*/
function websocketOpen(){
this.connectAck({confirm: true, reason: 'Connected'});
this.socket.send(JSON.stringify({ id: this.master.id }));
this.master.dispatch('connected', this);
}
function websocketMessage(evt){
handleReceive.call(this, evt.data);
}
function websocketError(){
this.connectAck({confirm: false, reason: 'Could not connect to peer'});
delete this.master.websockets[this.id];
}
/*
Communication Server
*/
function startServer(){
httpd.getURL(function(url){
if(url.length > 0) {
onServerUp.bind(this);
} else {
httpd.startServer({
'www_root' : this.options.wwwRoot,
'port' : this.options.port
}, onServerUp.bind(this), onServerError.bind(this) );
}
}.bind(this), onServerError.bind(this));
}
function stopServer(){
httpd.stopServer(function(){},function(){});
}
function onServerUp(url){
console.log('Server is up', url);
// Sending current IP to signaling server
this.signalingService.emit('signal:identification', { category: this.options.category, data: this.options.data, isServer: this.options.isServer, localAddress: url }, onIdentificationResponse.bind(this));
// Identify as peer to signaling server
this.dispatch('serverUp');
}
function onServerError(error){
alert('Httpd server error occured:', error);
this.dispatch('serverError');
}
function onServerConnected(socket){
var wrapper = makeSocketWrapper.call(this, socket, TrueSocial.WEBSOCKET_SERVER);
socket.receive = onServerIdentity.bind(wrapper);
socket.disconnect = handleClose.bind(wrapper);
}
function onServerIdentity(data){
console.log('onServerIdentity' + data);
data = JSON.parse(data);
this.id = data.id;
this.socket.receive = handleReceive.bind(this);
this.master.websockets[this.id] = this;
this.master.dispatch('connected', this);
}
/*
General Communication
*/
function makeSocketWrapper(socket, type, id){
var wrapper = new StageNetworkSocket(this, socket, type, id);
return wrapper;
}
function handleReceive(msg){
console.log('handleReceive', msg)
msg = JSON.parse(msg.replace(/\0/g, ''));
if(msg.ackCounter !== undefined){
msg.ack = function(){
var res = {
ackResponse: msg.ackCounter,
arguments: arguments,
}
this.socket.send(JSON.stringify(res));
}.bind(this);
} else if(msg.ackResponse !== undefined){
if(this.acks[msg.ackResponse]){
var args = [];
for (var key in msg.arguments) {
args.push(msg.arguments[key]);
};
this.acks[msg.ackResponse].apply(this, args);
delete this.acks[msg.ackResponse];
}
return;
}
this.master.dispatch(msg.key, msg.data, msg.ack, this.id);
this.dispatch(msg.key, msg.data, msg.ack);
}
function handleClose(){
this.master.dispatch('disconnect', this);
delete this.master.websockets[this.id];
this.dispatch('disconnect');
}
/*
Wrapper
*/
var StageNetworkSocket = function(master, socket, type, id){
this.listeners = {};
this.master = master;
this.socket = socket;
this.type = type;
this.id = id;
this.ackCounter = 0;
this.acks = [];
}
StageNetworkSocket.prototype.emit = StageNetworkSocket.prototype.send = function(key, data, ack) {
var msg = {
key: key,
data: data,
}
var getType = {};
if(ack && getType.toString.call(ack) === '[object Function]'){
msg.ackCounter = this.ackCounter;
this.acks[this.ackCounter] = ack;
this.ackCounter++;
// TODO add auto delete after a while
}
this.socket.send(JSON.stringify(msg));
};
/*
Accessories
*/
var EventBusClass = function () {}
EventBusClass.prototype.apply = function(object) {
object.addEventListener = EventBusClass.prototype.addEventListener;
object.on = EventBusClass.prototype.addEventListener;
object.hasEventListener = EventBusClass.prototype.hasEventListener;
object.removeEventListener = EventBusClass.prototype.removeEventListener;
object.off = EventBusClass.prototype.removeEventListener;
object.dispatch = EventBusClass.prototype.dispatch;
};
EventBusClass.prototype.addEventListener = function(type, callback, scope) {
var args = [];
var numOfArgs = arguments.length;
for(var i=0; i<numOfArgs; i++){
args.push(arguments[i]);
}
args = args.length > 3 ? args.splice(3, args.length-1) : [];
if(typeof this.listeners[type] != "undefined") {
this.listeners[type].push({scope:scope, callback:callback, args:args});
} else {
this.listeners[type] = [{scope:scope, callback:callback, args:args}];
}
};
EventBusClass.prototype.removeEventListener = function(type, callback, scope) {
if(typeof this.listeners[type] != "undefined") {
var numOfCallbacks = this.listeners[type].length;
var newArray = [];
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
if(listener.scope == scope && listener.callback == callback) {
} else {
newArray.push(listener);
}
}
this.listeners[type] = newArray;
}
};
EventBusClass.prototype.hasEventListener = function(type, callback, scope) {
if(typeof this.listeners[type] != "undefined") {
var numOfCallbacks = this.listeners[type].length;
if(callback === undefined && scope === undefined){
return numOfCallbacks > 0;
}
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
if((scope ? listener.scope == scope : true) && listener.callback == callback) {
return true;
}
}
}
return false;
};
EventBusClass.prototype.dispatch = function(type) {
var numOfListeners = 0;
var event = {
type:type,
};
var args = [];
var numOfArgs = arguments.length;
for(var i=1; i<numOfArgs; i++){
args.push(arguments[i]);
};
if(typeof this.listeners[type] != "undefined") {
var numOfCallbacks = this.listeners[type].length;
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
if(listener && listener.callback) {
var concatArgs = args.concat(listener.args);
listener.callback.apply(listener.scope, concatArgs);
numOfListeners += 1;
}
}
}
};
EventBusClass.prototype.getEvents = function() {
var str = "";
for(var type in this.listeners) {
var numOfCallbacks = this.listeners[type].length;
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
str += listener.scope && listener.scope.className ? listener.scope.className : "anonymous";
str += " listen for '" + type + "'\n";
}
}
return str;
};
EventBusClass.prototype.apply(TrueSocial.prototype);
EventBusClass.prototype.apply(StageNetworkSocket.prototype);
return TrueSocial;
})();