-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
571 lines (451 loc) · 16.8 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
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
var tako = require('tako')
, browserify = require('browserify')
, packagejson = require('./package.json')
, oppressor = require('oppressor')
, path = require('path')
, fs = require('fs')
, EventEmitter = require('events').EventEmitter
, persist = require('./lib/persist')()
, model = require('./lib/models')
, changesbus = require('./lib/changesbus')
, cookie = require('cookie')
, _ = require('underscore')
;
module.exports = function(config){
var em = new EventEmitter();
config = config||{};
//
// create tako
//
var app = tako()
em.routes = function(){
//
// static content
//
app.route('/static/*').files(path.join(__dirname, 'static'));
//
// shim fake index
//
var index = fs.readFileSync('./static/mock.html');
app.route('/').html(index);
//
// add the bundle
//
app.route('/client.js',function(req,res){
var out = bundle('app.js','client/app.js');
res.setHeader('Content-Type','text/javascript');
res.setHeader('Expires',(new Date(0)).toString());
var oppress = oppressor(req);
oppress.pipe(res);
oppress.end(out);
});
app.route('/media.js',function(req,res){
var out = bundle('media.js','media.js');
var oppress = oppressor(req);
oppress.pipe(res);
oppress.end(out);
});
app.route('/package').json(JSON.stringify(packagejson)+"\n");
};
em._sockets = {};
em._rooms = model('rooms');
em.sockets = function(){
var z = this;
z.app.sockets.manager.settings['log level'] = 2;
z.app.sockets.manager.settings['authorization'] = function(data,cb){
var memberid;
if(data.headers.cookie){
var cookies = cookie.parse(data.headers.cookie);
memberid = cookies['clever-sid'];
}
persist.getMember(memberid,function(err,member){
if(err && err.status_code != 404) console.log('PERSIST error getMemberById ',err);
if(member) {
// can check if another socket is still this person
data.member = member;
member.set('updated',Date.now());
return cb(null,true);
}
member = model('member');
member.save(function(err,model){
if(err) return cb('sorry we had an issue generating a new membership',false)
console.log(member,model);
data.member = member;
cb(null,true);
});
});
};
z.app.sockets.on('connection', function(socket) {
socket.on('laffs', function(){
socket.emit('laffs')
});
socket.on('horn', function(){
socket.emit('horn')
});
socket.on('applause', function(){
socket.emit('applause')
});
z._sockets[socket.id] = model('socket',{socket:socket});
z._sockets[socket.id].set('member', socket.handshake.member);
// set the inital room to be the last room this member was in
z._sockets[socket.id].set('room',socket.handshake.member.get('room'));
z._sockets[socket.id].save(function(err,data) {
var socketStateData = z.getSocketsData();
var roomStateData = modelData(z._rooms.get('rooms'));
console.log('67646535555555555%%%%%%%%%%%%%%%%% ',roomStateData.length,roomStateData);
if(roomStateData.length > 50) {
roomStateData = roomStateData.slice(roomStateData.length-50);
}
socket.emit('connected', {rooms:roomStateData,sockets:socketStateData,id:socket.handshake.member.get('id'),member:socket.handshake.member.getData()});
// Prompt them to join a room / rejoin the last room they were in.
var oldRoom = z._sockets[socket.id].get('room')||false;
socket.emit('choose_room',{room:oldRoom});
});
socket.on('disconnect', function() {
z.emit('disconnect', socket.id);
var socketRoomId = z._sockets[socket.id].get('room');
if(socketRoomId) {
var room = z._rooms.findRoom(socketRoomId);
if(room) {
var memberModel = z._sockets[socket.id].get('member')
, memberIdToRemove = memberModel.get('id')
, previousServerMemberId = room.get('server')
, newServerMemberId
;
room.removeMember(memberIdToRemove);
newServerMemberId = room.get('server');
// The member removed was the server. Let the new server member know to start streaming for everyone else.
if(newServerMemberId && newServerMemberId != previousServerMemberId) {
z.changeFeedSource(newServerMemberId);
}
// Let everyone else in the room know to remove this member.
for(var socketId in z._sockets) {
if(z._sockets[socketId].get('room') == room.get('id')) {
z._sockets[socketId].get('socket').emit('remove_member', {id: memberIdToRemove});
}
}
}
}
delete z._sockets[socket.id];
});
socket.on('choose_feed_source', function(data, cb) {
z.changeFeedSource(data.connectionId);
});
// a client wants to refetch rooms.
socket.on('get_rooms',function(cb){
if(!cb || !cb.call) return;
cb(null,modelData(z._rooms.get('rooms')));
});
socket.on('create_room', function(data, cb) {
z.createRoom(socket, data, function(err,data){
if(err) return cb(err+' failed to create room.');
var soc = em._socketData(z._sockets[socket.id]);
socket.emit('add_member', {id:soc.member.id,socket:soc});
});
});
socket.on('join_room', function(data, cb) {
var room = z._rooms.findRoom(data.id)
, newMemberId = z._sockets[socket.id].get('member').get('id')
;
var gotRoom = function(room){
console.log('gotRoom called with ',room);
// Tell the current video server user about the new user, if that isn't the new user.
var currentRoomServer = room.get('server');
console.log('CURRENT ROOOM SERVER ',currentRoomServer,newMemberId);
if(currentRoomServer != newMemberId) {
var roomServerSocket = z.getSocketBySocketMemberId(currentRoomServer);
if(roomServerSocket !== null) {
roomServerSocket.get('socket').emit('start_server', {connectionIds: [newMemberId]});
}
}
// Let the new user know about everyone.
console.log('GET MEMBERS!!! ',room.get('members'));
room.get('members').forEach(function(memberId) {
var s = z.getSocketBySocketMemberId(memberId);
var soc = em._socketData(s);
console.log('add_member',soc.member.id);
socket.emit('add_member', {id:soc.member.id,socket:soc});
});
// Let the existing users each know about the new user.
var mySocInfo = em._socketData(z._sockets[socket.id]);
for(var socketId in z._sockets) {
if(z._sockets[socketId].get('room') == room.get('id')) {
z._sockets[socketId].get('socket').emit('add_member', {id:mySocInfo.member.id,socket:mySocInfo});
}
}
if(cb) cb(null,room);
};
if(room) {
room.push('members', newMemberId);
if(!room.get('director')) {
room.set('director', newMemberId);
}
if(!room.get('server')) {
room.set('server', newMemberId);
}
// Update the user's room.
z._sockets[socket.id].set('room', room.get('id'));
z._sockets[socket.id].get('member').set('room', room.get('id'));
console.log('Added user to existing room: ', room);
gotRoom(room);
} else {
console.log('im going ot create a room')
z.createRoom(socket, {director:newMemberId,server:newMemberId,title:data.title||data.name,description:data.description}, function(err,room){
console.log('Added user to new room: ', room);
if(err) cb(err+' failed to create room to join.');
gotRoom(room);
});
}
});
socket.on('server_ready', function(data, cb) {
console.log('server_ready', data);
// This means a video server client has prepared the PeerConnections for the rest of the room users.
if(data.connectionIds) {
var socketRoom = z._rooms.findRoom(z._sockets[socket.id].get('room'));
if(socketRoom) {
var serverMemberId = z._sockets[socket.id].get('member').get('id');
socketRoom.get('members').forEach(function(memberId) {
if(memberId != serverMemberId) {
var inReadyConnections = false;
for(var i = 0; i < data.connectionIds.length; i++) {
if(data.connectionIds[i] == memberId) {
inReadyConnections = true;
break;
}
}
if(inReadyConnections) {
var memberSocket = z.getSocketBySocketMemberId(memberId);
if(memberSocket !== null) {
memberSocket.get('socket').emit('switch_to_server', {id: serverMemberId});
}
}
}
});
}
}
});
socket.on('offer', function(data, cb) {
console.log('offer', data);
// Forward an offer from this client to the current server user for this client's room.
var socketRoom = z._rooms.findRoom(z._sockets[socket.id].get('room'));
if(socketRoom) {
var memberId = z._sockets[socket.id].get('member').get('id'),
serverMemberId = socketRoom.get('server'),
serverMemberSocket = z.getSocketBySocketMemberId(serverMemberId);
if(memberId != serverMemberId && serverMemberSocket !== null) {
// Augment the data with the id of the member the offer came from,
// so the server can associate it with a connection.
data.connectionId = memberId;
serverMemberSocket.get('socket').emit('offer', data);
}
}
});
socket.on('answer', function(data, cb) {
console.log('answer', data);
// Forward an answer from this server to the designated recipient.
if(data.connectionId) {
var clientSocket = z.getSocketBySocketMemberId(data.connectionId);
if(clientSocket !== null) {
delete data.connectionId;
clientSocket.get('socket').emit('answer', data);
}
}
});
socket.on('candidate', function(data, cb) {
console.log('candidate', data);
// Forward candidate messages to the appropriate recipient.
var socketRoom = z._rooms.findRoom(z._sockets[socket.id].get('room'));
if(socketRoom) {
var serverMemberId = socketRoom.get('server');
if(serverMemberId) {
var socketMemberId = z._sockets[socket.id].get('member').get('id');
console.log(serverMemberId,'< server member id: socket member id>',socketMemberId,'connection id',data.connectionId);
if(serverMemberId == socketMemberId) {
var clientSocket = z.getSocketBySocketMemberId(data.connectionId);
console.log('have client socket? ',clientSocket);
if(clientSocket !== null) {
delete data.connectionId;
clientSocket.get('socket').emit('candidate', data);
}
} else if(serverMemberId != socketMemberId) {
var serverSocket = z.getSocketBySocketMemberId(serverMemberId);
console.log('have server!!! socket? ',serverSocket);
if(serverSocket !== null) {
data.connectionId = socketMemberId;
serverSocket.get('socket').emit('candidate', data);
}
}
}
}
});
z.emit('connection', z._sockets[socket.id]);
});
};
em.getSocketBySocketMemberId = function(socketMemberId) {
var socket = null;
for(var socketId in em._sockets) {
if(socketMemberId == em._sockets[socketId].get('member').get('id')) {
socket = em._sockets[socketId];
}
}
console.log('found socket ',socket,'for member id ',socketMemberId);
return socket;
};
em.getSocketsData = function(){
var data = [];
for(var socketId in em._sockets) {
data.push(em._socketData(em._sockets[socketId]));
}
return data;
}
em._socketData = function(socket){
var soc = _.extend({},socket.getData());
soc.member = _.extend({},soc.member.getData());
delete soc.socket;
return soc;
}
em.createRoom = function(socket, data, cb) {
// IF ID IS provided look it up. dont just let anyone insert crazy ids.
//if(data.id && data.id.indexOf('room_') !== 0) {
// delete data.id;
//}
var room = model('room', data)
, socketModel = em._sockets[socket.id]
, memberModel = socketModel.get('member')
;
room.set('director', memberModel.get('id'));
room.set('server', memberModel.get('id'));
room.set('members', [memberModel.get('id')]);
console.log('ABOUT TO ROOM SAVE!!!');
room.save(function(err, data) {
//console.log('ROOM AVE CALLED BACK!! ',err,data);
if(cb) {
cb(err,room);
}
if(err) return;
em._rooms.push('rooms', room);
em._rooms.get('rooms').forEach(function(otherRoom) {
if(room.get('id') === otherRoom.get('id')) {
return;
}
// Remove director from any current rooms.
if(room.get('director') === otherRoom.get('director')) {
otherRoom.removeMember(otherRoom.get('director'));
}
});
// Update the socket's current room.
socketModel.set('room', room.get('id'));
memberModel.set('room',room.get('id'));
});
};
em.changeFeedSource = function(newServerMemberId) {
var newServerMemberSocket = em.getSocketBySocketMemberId(newServerMemberId);
if(newServerMemberSocket !== null) {
var roomId = newServerMemberSocket.get('member').get('room'),
room = em._rooms.findRoom(roomId);
if(room) {
room.set('server', newServerMemberId);
var roomMembers = room.get('members'),
membersExceptNewServer = [];
for(var i = 0; i < roomMembers.length; i++) {
if(roomMembers[i] != newServerMemberId) {
membersExceptNewServer.push(roomMembers[i]);
}
}
console.log("Changing feed source for " + room.get('id') + " to " + newServerMemberId);
newServerMemberSocket.get('socket').emit('start_server', {connectionIds: membersExceptNewServer});
}
}
};
//TODO -----------
em.visitRoom = function(socket,data,cb){
var socketModel = em._sockets[socket.id]
, memberModel = socketModel.get('member')
, currentRoomId = socketModel.get('room')||memberModel.get('room')
, roomId = data.id
;
z._rooms.forEach(function(r){
});
};
em.observeSelf = function(){
var z = this;
z.on('disconnect', function() {
console.log('socket disconnected');
});
};
em.loadRooms = function(cb){
persist.getRooms(function(err,data){
em._rooms = data?data:model('rooms');
cb(err,data);
});
}
//
// listen.
//
em.listen = function(port,cb){
app.httpServer.listen(port,cb);
};
em.app = app;
em.observeSelf();
em.routes();
em.loadRooms(function(){
console.log('loaded rooms.');
em.sockets();
});
// room inactive
// member left
// room changed
//
var updating = {};
changesbus.on('change',function(ev,model){
var id = model.get('id');
var type = model.type;
//console.log("P: ",ev,' on ',type,id);
if(!model.dirty) {
//console.log('the model is not dirty!');
return;
}
if(!model.get('id')) {
//console.log('the model has no id!');
console.log("P: ",model.type,' has no object id yet.');
return;
}
if(type == 'member') {
if(ev == 'set') {
persist.setMember(model,function(err,data){
// member was updated?
if(err) console.log('P: MEMBER persisted ',id,err);//,data);
});
}
} else if(type == 'room'){
//console.log('stype is room!!');
if(ev == 'push' || ev == 'set' || ev == 'save') {
persist.setRoom(model,function(err,data){
// member was updated?
if(err) console.log('P: room persisted ',id,err,data);
});
}
}
});
return em;
}
function bundle(js,module,config){
config = config||{};
if(!bundle.bundles) bundle.bundles = {};
var out = bundle.bundles[js];
if(!out){
out = '';
var b = browserify()
b.require(__dirname+'/client/'+js);
out = b.bundle();
out += ";require('./"+module+"')("+JSON.stringify(config.client||{})+");";
bundle.bundles[js] = out;
}
return out;
}
// dump an array of model's data objects
function modelData(models){
return models.map(function(model){
return model.getData();
});
}