-
Notifications
You must be signed in to change notification settings - Fork 709
/
server.js
191 lines (163 loc) · 8.02 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
/*
Copyright (c) 2012, Priologic Software Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
// All server configuration is done in config.js
easyRtcConfig = require('./config');
// Module dependencies
var express = require('express');
var sio = require('socket.io');
// Set express http server options. Ensure static folder is not in the root.
var httpApp = express();
httpApp.configure(function() {
httpApp.use(express.static(__dirname + '/static/'));
});
// Start HTTP and socket server
var server = httpApp.listen(easyRtcConfig.httpPort);
var io = sio.listen(server);
// io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
console.log('HTTP and Socket Server started on port: ' + easyRtcConfig.httpPort);
// Start experimental STUN server (if enabled)
if (easyRtcConfig.experimentalStunServerEnable) {
var stunLib = require('./lib/stunserver');
var stunServer = stunLib.createServer();
stunServer.setAddress0(easyRtcConfig.experimentalStunServerAddr0);
stunServer.setAddress1(easyRtcConfig.experimentalStunServerAddr1);
stunServer.setPort0(easyRtcConfig.experimentalStunServerPort0);
stunServer.setPort1(easyRtcConfig.experimentalStunServerPort1);
stunServer.listen();
}
// Shared variable to hold server and socket information.
var easyRtc = {
serverStartTime: Date.now(),
connections: {}
};
// Upon a socket connection, a socket is created for the life of the connection
io.sockets.on('connection', function (socket) {
// console.log('easyRTC: User: ' + socket.id + ' Application: ' + easyRtcConfig.defaultApplicationName);
var connectionEasyRtcId = socket.id;
easyRtc.connections[connectionEasyRtcId]={
easyrtcid: connectionEasyRtcId,
applicationName: easyRtcConfig.defaultApplicationName,
clientConnectTime: Date.now()
};
// Immediatly send the easyrtcid and application
socket.json.emit( easyRtcConfig.cmdPacketType, {
msgType:easyRtcConfig.cmdMsgType.token,
easyrtcid: connectionEasyRtcId,
applicationName: easyRtc.connections[connectionEasyRtcId].applicationName,
iceConfig: {"iceServers": easyRtcConfig.iceServers},
serverTime: Date.now()
});
// Send the connection list to current connection, then broadcast to all others
broadcastList(easyRtc.connections[connectionEasyRtcId].applicationName);
// console.log('easyRTC: Socket connected: ' + socket.id);
// Incoming messages: Custom message. Allows applications to send socket messages to other connected users.
socket.on('message', function(msg) {
// Messages must have a targetId and a msgType. This section should be hardened.
if (msg.targetId) {
// console.log('easyRTC: Custom Message: ' + msg.msgData);
io.sockets.socket(msg.targetId).json.send({
msgType:msg.msgType,
senderId:connectionEasyRtcId,
msgData:msg.msgData,
serverTime: Date.now()
});
}
});
// Incoming Messages: easyRTC commands. Used to forward webRTC negotiation details and manage server settings.
socket.on('easyRTCcmd', function(msg) {
// Messages with a targetId get forwarded on. This section should be hardened.
if (msg.targetId) {
// console.log('easyRTC: Command: ' + msg.msgType + ' To: ' + msg.targetId + ' From: ' + connectionEasyRtcId);
io.sockets.socket(msg.targetId).json.emit('easyRTCcmd',{
msgType:msg.msgType,
senderId:connectionEasyRtcId,
msgData:msg.msgData,
serverTime: Date.now()
});
}
// easyRtc server-side user configuration options are set here.
if (msg.msgType == "setUserCfg") {
// console.log('easyRTC: Command: ' + msg.msgType + ' From: ' + connectionEasyRtcId);
if (msg.msgData.applicationName) { // Set the application namespace
easyRtc.connections[connectionEasyRtcId].applicationName = msg.msgData.applicationName;
// console.log('easyRTC: Application Name Change: ' + connectionEasyRtcId + ' : ' + msg.msgData.applicationName);
broadcastList(easyRtc.connections[connectionEasyRtcId].applicationName);
}
}
});
// Upon a socket disconnecting (either directed or via time-out)
socket.on('disconnect', function(data) {
// console.log('easyRTC: Socket disconnected: ' + connectionEasyRtcId);
var previousApplicationName = easyRtc.connections[connectionEasyRtcId].applicationName;
// Remove connection from the map
delete easyRtc.connections[connectionEasyRtcId];
// Broadcast new list to all others
broadcastList(previousApplicationName);
});
// Send a list of all connected clients to a specific user
// This function is due for a major overhaul to improve its usefullness.
function sendList(easyRtcId) {
io.sockets.socket(easyRtcId).json.emit('easyRTCcmd',{
msgType:easyRtcConfig.cmdMsgType.list,
msgData:easyRtc,
serverTime: Date.now()
});
};
// Broadcast a list of all connected clients to all connected users (except current client).
// This function is due for a major overhaul to improve its usefullness.
function broadcastList(applicationName) {
var listData = {
serverStartTime: easyRtc.serverStartTime,
connections: {}
};
// Form list of current connections
for (var key in easyRtc.connections) {
if (easyRtc.connections.hasOwnProperty(key) && easyRtc.connections[key].applicationName == applicationName) {
listData.connections[key] = easyRtc.connections[key];
}
};
// Broadcast list of current connections
for (var key in listData.connections) {
io.sockets.socket(key).json.emit('easyRTCcmd',{
msgType:easyRtcConfig.cmdMsgType.list,
msgData:listData,
serverTime: Date.now()
});
};
};
});
// Checks to see if there is a newer version of easyRTC available
if (easyRtcConfig.updateCheckEnable) {
http = require('http');
http.get("http://easyrtc.com/version/?app=easyrtc&ver=" + easyRtcConfig.easyRtcVersion + "&platform=" + process.platform, function(res) {
if (res.statusCode == 200)
res.on('data', function(latestVersion) {
latestVersion = (latestVersion+"").replace(/[^0-9a-z.]/g,"");
if (latestVersion != easyRtcConfig.easyRtcVersion)
console.log("Note: New version of easyRTC is available (" + latestVersion + "). Visit http://easyrtc.com/");
});
}).on('error', function(e){});
}