-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (60 loc) · 1.78 KB
/
app.js
File metadata and controls
72 lines (60 loc) · 1.78 KB
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
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, routes = require('./routes')
, path = require('path')
, mu2Express = require('mu2express');
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.engine('mustache', mu2Express.engine);
app.set('view engine', 'mustache');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.webrtc);
app.get('/:id', routes.webrtc);
var sessions = {};
var sockets = {};
//io.set('log level', 1);
io.sockets.on('connection', function (socket) {
socket.on('desc', function(data) {
sockets[data.id][data.client_id].emit('add_desc', data);
});
socket.on('cand', function(data) {
sockets[data.id][data.client_id].emit('add_cand', {'cand': data.cand, 'client_id': data.from_client_id});
});
socket.on('join', function(data) {
if (data.id !== '' && sockets[data.id] !== undefined) {
client_id = sockets[data.id].length;
id = data.id;
} else if (data.id != '') {
id = data.id;
client_id = 0;
sockets[id] = [];
} else {
var id;
do {
id = Math.floor(Math.random()*1000000);
} while (id in sockets);
client_id = 0;
sockets[id] = [];
}
sockets[id].push(socket);
socket.emit('joined', {'client_id': client_id, 'id': id});
});
});
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});