-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
235 lines (182 loc) · 5.49 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
/**
* @author Marenin Alex
* October 2013
*/
var commander = require( 'commander' );
commander
.usage( '[options]' )
.option( '-p, --port [port]', 'Set Web server port', false )
.option( '-c, --config [config]', 'Set the config name to use, default is "dev"', 'dev' )
.parse( process.argv );
var util = require( './util' ),
http = require( 'http' ),
express = require( 'express' ),
config = util.getConfig( commander.config ),
socket = require( 'socket.io' ),
socketUrl = config.protocol + '://' + config.host + ':' + config.externalPort;
config.port = commander.port || config.port;
var app = express(),
server = http.createServer( app ),
io = socket.listen( server );
app.disable( 'x-powered-by' );
app.set( 'trust proxy', config.proxyUsed );
app.set( 'views', './view' );
app.set( 'view engine', 'jade' );
app.locals.isDebug = config.debug;
app.use( express.bodyParser() );
app.use( express.methodOverride() );
app.use( config.debug ? express.logger('dev') : express.logger() );
app.use( '/public', express.static('./public') );
app.use( express.favicon('public/favicon.ico') );
app.use( app.router );
server.listen( config.port, function(){
console.log( 'HTTP server is listening port ' + config.port );
});
// HTTP
app.get( '/', function( req, res ){
res.render( 'index', {
socketUrl: socketUrl
});
});
app.get( '/r/:id', function( req, res ){
var id = req.params.id,
room = rooms[id];
if ( room )
res.render( 'index', {
socketUrl: socketUrl,
roomName: room.name,
needAuth: room.isPublic(),
error: room.isFull() ? 'Room is full' : ''
});
else
res.render( 'index', {
socketUrl: socketUrl,
error: 'Room "' + id + '" doesn\'t exists'
});
});
// SOCKET
var rooms = {},
peers = {};
io.sockets.on( 'connection', function( socket ){
var peer;
socket.on( 'disconnect', function(){
var room = peer.getRoom();
if ( room ){
room.removePeer( peer );
if ( room.isEmpty() )
delete rooms[room.name];
}
delete peers[peer.getId()];
});
socket.on( 'initUser', function( data ){
peer = new Peer( data.id, socket );
peers[peer.getId()] = peer;
});
socket.on( 'createRoom', function( data, cb ){
var name = data.name;
if ( name ){
if ( rooms[name] ){
cb( {error: 'Room already exists'} );
return;
}
}
else do
name = util.generateId(6);
while ( rooms[name] );
var room = new Room( name, data.password || false, peer );
rooms[name] = room;
peer.setRoom( room );
cb( {name: room.name, peers: room.getPeerIds()} );
});
socket.on( 'joinRoom', function( data, callback ){
var name = data.name,
room = name && rooms[name];
if ( !name )
callback( {error: 'Room name cannot be empty'} );
else if ( !room )
callback( {error: 'Unable to find room with such name'} );
else if ( !room.isPasswordCorrect(data.password) )
callback( {error: 'Wrong password'} );
else if ( room.isFull() )
callback( {error: 'Room is full'} );
else {
room.addPeer( peer );
peer.setRoom( room );
callback( {name: room.name, peers: room.getPeerIds()} );
}
});
socket.on( 'offer', function( offer, callback ){
var remotePeerId = offer.id,
remotePeer = remotePeerId && peers[remotePeerId];
if ( !remotePeer )
callback( {error: 'There is no remote peer'} );
else
remotePeer.socket.emit( 'offer', {id: peer.getId(), sdp: offer.sdp}, function( answer ){
if ( answer.reject )
callback( {reject: true} );
else
callback( {sdp: answer.sdp} );
});
});
0
socket.on( 'iceCandidate', function( data ){
var remotePeer = peers[data.id];
if ( remotePeer )
remotePeer.socket.emit( 'iceCandidate', {id: peer.getId(), candidate: data.candidate} );
});
});
/**
* @constructor
*/
function Room( name, password, ownerPeer ){
this.name = name;
this.password = password;
this.peers = [];
this.addPeer( ownerPeer );
}
Room.prototype.isPublic = function(){
return !this.password;
};
Room.prototype.isPasswordCorrect = function( pass ){
return !this.password || pass === this.password;
};
Room.prototype.isFull = function(){
return this.peers.length >= config.roomCapacity;
};
Room.prototype.isEmpty = function(){
return this.peers.length === 0;
};
Room.prototype.addPeer = function( peer ){
var id = peer.getId();
if ( !this.isFull() && this.peers.indexOf(id) === -1 )
this.peers.push( id );
};
Room.prototype.removePeer = function( peer ){
var id = peer.getId(),
i = this.peers.indexOf( id );
if ( i > -1 )
this.peers.splice( i, 1 );
};
Room.prototype.getPeerIds = function(){
return this.peers;
};
/**
* @constructor
*/
function Peer( id, socket ){
this.id = id;
this.socket = socket;
}
Peer.prototype.getId = function(){
return this.id;
};
Peer.prototype.setRoom = function( room ){
this.room = room;
};
Peer.prototype.getRoom = function(){
return this.room;
};
Peer.prototype.destroy = function(){
delete this.room;
delete this.socket;
};