-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
203 lines (177 loc) · 7.77 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
/* For security reasons the SSL certificate, its private key and the Microsoft Translator key are not included.*/
// Start signal
console.log('>> BaBL:', new Date(), '- Server started');
/////////////////////////////////////////////
//WEB SERVER:
// DEPENDENCIES:
var http = require('http');
var https = require('https');
var fs = require('fs');
var static = require('node-static');
var file = new static.Server();
//SSL CERTIFICATE:
var httpsoptions = {
key: fs.readFileSync('PRIVATE KEY HERE.key'),
cert: fs.readFileSync('SSL CERTIFICATE HERE.crt')
};
//HTTPS WEB SERVER:
var HTTPSWebServer = https.createServer(httpsoptions, function(req, res) {
//Serve room requests
if (req.url.substring(1, 6) === 'room=' && req.url.indexOf('&username=') !== 6) {
file.serveFile('/room.html', 200, {}, req, res);
//Serve error404.html for hidden files
} else if (req.url === 'PRIVATE KEY HERE.key' || req.url === 'SSL CERTIFICATE HERE.crt') {
file.serveFile('/error404.html', 404, {}, req, res);
//Serve the rest of the files and handles 404 errors
} else {
file.serve(req, res, function(error, errorRes) {
if (error && (error.status === 404)) {
file.serveFile('/error404.html', 404, {}, req, res);
}
});
}
}).listen(443);
//HTTP WEB SERVER: Redirects all traffic to HTTPS server
var HTTPWebServer = http.createServer(function(req, res) {
res.writeHead(301, {'Location': 'https://dixie11.rice.iit.edu' + req.url});
res.end();
}).listen(80);
/////////////////////////////////////////////
//WEBRTC SIGNALING SERVER:
// VARIABLES:
var rooms = {};
// SOCKET.IO SET-UP:
var io = require('socket.io').listen(HTTPSWebServer);
// CONNECTION HANDLING:
io.sockets.on('connection', function(socket) {
var clientAddress = socket.handshake.address;
console.log('>> BaBL:', new Date(), '- Client connected: {', socket.id, '} @', clientAddress);
// Handles join requests
socket.on('request to join', function(username, room) {
// Checks the params are valid
if (!username || !room) {
console.log('>> BaBL:', new Date(), '- BAD PARAMS FROM SOCKET ID', socket.id);
socket.disconnect();
} else {
console.log('>> BaBL:', new Date(), '- User', username, 'requests to join room', room);
//Creates the room if it is not defined
if (rooms[room] === undefined) {
rooms[room] = {};
socket.join(room);
socket.socketID = username + '@' + room;
socket.emit('room created', room);
rooms[room][username] = socket;
console.log('>> BaBL:', new Date(), '- Room', room, 'created by user', username);
//Test if the username is already in use in the room
} else if (Object.keys(rooms[room]).indexOf(username) !== -1) {
console.log('>> BaBL:', new Date(), '- Username', username, 'already in use in room', room);
log('Username', username, 'already in use in room', room);
socket.emit('username in use', username, room);
socket.disconnect();
//Connect to the room
} else {
// Let the other users know that this user has joined
io.sockets.in(room).emit('new user joined', username, room);
socket.join(room);
socket.socketID = username + '@' + room;
// Previous userlist is attached for peer connection creation
socket.emit('room joined', room, Object.keys(rooms[room]));
rooms[room][username] = socket;
console.log('>> BaBL:', new Date(), '- Username', username, 'joined room', room);
}
}
});
// Disconnection handling
socket.on('disconnect', function() {
var username;
var room;
if (socket.socketID !== null) {
username = socket.socketID.split('@')[0];
room = socket.socketID.split('@')[1];
delete rooms[room][username];
if (Object.keys(rooms[room]).length === 0) {
delete rooms[room];
}
socket.leave(room);
console.log('>> BaBL:', new Date(), '- User', username, 'left room', room);
socket.broadcast.to(room).emit('user disconnected', username);
}
var clientAddress = socket.handshake.address;
console.log('>> BaBL:', new Date(), '- Client disconnected: {', socket.id, '} @', clientAddress);
});
// SUBTITLES:
// Redirects subtitles requests
socket.on('subtitles request', function(message, toUser, language) {
var fromUser = socket.socketID.split('@')[0];
var room = socket.socketID.split('@')[1];
// Avoids server crashes
if (typeof (rooms[room][toUser]) !== 'undefined') {
rooms[room][toUser].emit('subtitles request', message, fromUser, language);
} else {
console.log('>> BaBL:', new Date(), '- BAD PARAMS FROM SOCKET ID', socket.id);
socket.disconnect();
}
});
// TRANSLATION:
// Process translation requests
socket.on('translation request', function(subtitleToTranslate, fromLanguage, toLanguage, toUser) {
if (!subtitleToTranslate.text) {
console.log('>> BaBL:', new Date(), '- BAD PARAMS FROM SOCKET ID', socket.id);
socket.disconnect();
} else {
var translationRequest = {
text: subtitleToTranslate.text,
from: fromLanguage,
to: toLanguage
};
var fromUser = socket.socketID.split('@')[0];
var room = socket.socketID.split('@')[1];
charactersTranslated += subtitleToTranslate.text.length;
console.log('>> BaBL:', new Date(), '-', charactersTranslated,
'characters translated since last server start');
client.translate(translationRequest, function(err, data) {
// Avoids server crashes
if (typeof (rooms[room][toUser]) !== 'undefined') {
rooms[room][toUser].emit('translation', data, fromUser, subtitleToTranslate.isFinal);
} else {
console.log('>> BaBL:', new Date(), '- BAD PARAMS FROM SOCKET ID', socket.id);
socket.disconnect();
}
});
}
});
// FUNCTIONS:
// Redirects a user's message to the rest of users in one room
socket.on('message to room', function(message) {
var fromUser = socket.socketID.split('@')[0];
var toRoom = socket.socketID.split('@')[1];
socket.broadcast.to(toRoom).emit('message', message, fromUser);
});
// Redirects a user's message to other user in one room
socket.on('message to user', function(message, toUser) {
var fromUser = socket.socketID.split('@')[0];
var room = socket.socketID.split('@')[1];
// Avoids server crashes
if (typeof (rooms[room][toUser]) !== 'undefined') {
rooms[room][toUser].emit('message', message, fromUser);
}
});
// Function for sending messages to the client's console
function log() {
var array = ['>> BaBL server message:'];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
});
/////////////////////////////////////////////
//TRANSLATION SERVICE:
// VARIABLES:
var MsTranslator = require('mstranslator');
var client = new MsTranslator({client_id: 'BaBL', client_secret: 'MICROSOFT TRANSLATOR KEY HERE'});
var charactersTranslated = 0;
// EXECUTION:
// Translation initialization
client.initialize_token();
console.log('>> BaBL:', new Date(), '- Translation service initialized');