-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
executable file
·388 lines (346 loc) · 14.3 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
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
#!/bin/env node
var express = require('express');
var fs = require('fs');
var makeChat = require('./static/chat.js');
/**
* Define the sample application.
*/
var ChatApp = function() {
// Scope.
var self = this;
/**
* Set up server IP address and port # using env variables/defaults.
*/
self.setupVariables = function() {
// Set the environment variables we need.
self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
self.port = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_IP || 8000;
if (typeof self.ipaddress === "undefined") {
// Log errors on OpenShift but continue w/ 127.0.0.1 - this
// allows us to run/test the app locally.
console.warn('No OPENSHIFT_INTERNAL_IP var, using 127.0.0.1');
self.ipaddress = "127.0.0.1";
};
console.log(self.ipaddress + ':' + self.port);
};
/**
* Populate the cache.
*/
self.populateCache = function () {
if (typeof self.zcache === "undefined") {
self.zcache = { 'index.html': '' };
}
// Local cache for static content.
for (var i=0; i<self.static_files.length; i++) {
self.zcache[self.static_files[i]] = fs.readFileSync('./static/' + self.static_files[i]);
}
};
self.refreshCache = function () {
for (var i=0; i<self.static_files.length; i++) {
(function () {
var path = self.static_files[i];
fs.readFile('./static/' + path, function (error, data) {
if (!error) {
self.zcache[path] = data;
}
});
}());
}
};
self.dirFiles = function (dir_path) {
var file_list = [];
var dir_items = fs.readdirSync(dir_path);
for (var i=0; i<dir_items.length; i++) {
var stats = fs.statSync(dir_path + dir_items[i]);
if (stats.isFile()) {
file_list.push(dir_items[i]);
}
else {
// dir_items[i] is a directory
}
}
return file_list;
}
/**
* Retrieve entry (content) from cache.
* @param {string} key Key identifying content to retrieve from cache.
*/
self.cache_get = function(key) { return self.zcache[key]; };
/**
* terminator === the termination handler
* Terminate server on receipt of the specified signal.
* @param {string} sig Signal to terminate on.
*/
self.terminator = function(sig){
if (typeof sig === "string") {
console.log('%s: Received %s - terminating sample app ...',
Date(Date.now()), sig);
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()) );
};
/**
* Setup termination handlers (for exit and a list of signals).
*/
self.setupTerminationHandlers = function(){
// Process on exit and signals.
process.on('exit', function() { self.terminator(); });
// Removed 'SIGPIPE' from the list - bugz 852598.
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { self.terminator(element); });
});
};
self.createRoutes = function() {
self.routes = { };
// Routes for /health, /asciimo and /
self.routes['/health'] = function(req, res) {
res.send('1');
};
self.routes['/'] = self.createStaticRoute('index.html');
// Create new chat and send redirect to it
self.routes['/new/*'] = function (req, res) {
res.setHeader('Content-Type', "application/json");
var slice_point = req.url.lastIndexOf('/new/');
var chat_url = req.url.slice(slice_point + 5);
if (slice_point >= 0) {
if (self.chats[chat_url]) {
res.send({error: 'That chat name is already in use'});
}
else {
var chat_name = chat_url.split('_').join(' ');
self.chats[chat_url] = self.createChat(chat_name);
res.send({redirect: chat_url});
}
}
else {
res.send({error: 'No chat name specified in ' + req.url});
}
};
/*
// Add routes for uncached image files
for (var i=0; i<self.image_files.length; i++) {
console.log('Creating uncached route for ' + self.image_files[i]);
self.routes['/' + self.image_files[i]] = self.createUncachedRoute('./images/' + self.image_files[i]);
}
*/
// Add routes for static files in cache
for (var i=0; i<self.static_files.length; i++) {
console.log('Creating cached route for ' + self.static_files[i]);
self.routes['/' + self.static_files[i]] = self.createStaticRoute(self.static_files[i]);
}
self.routes['/*'] = function (req, res) {
var slice_point = req.url.indexOf('?');
if (slice_point < 0) {
// undefined slice point means substring will continue to end of string
slice_point = undefined;
}
var chat_url = req.url.substring(1,slice_point);
console.log(chat_url + ' requested');
if (self.chats[chat_url]) {
self.createStaticRoute('chat.html')(req, res);
}
else {
res.status(302);
console.log('redirecting to https://' + req.headers.host);
res.setHeader('Location', 'https://' + req.headers.host);
res.setHeader('Content-Type', 'text/html');
var error_page = '<html><body style="';
error_page += 'text-align: center; color: #444448; background-color: #EEEEF3; font-family: sans-serif';
error_page += '"><h2>Sorry</h2><p>The page could not be found</p></body></html>'
res.send(error_page);
}
}
};
self.createUncachedRoute = function(uncached_file) {
return function(req, res) {
var mime_type = self.mimeType[uncached_file.substring(uncached_file.lastIndexOf('.') + 1)];
if (mime_type === undefined) {
mimeType = "text/plain";
}
res.setHeader('Content-Type', mime_type);
fs.readFile(uncached_file, function (err, data) {
if (err) {
console.log('Unable to read file ' + uncached_file);
res.send("");
}
else {
res.send(data);
}
});
}
}
self.createStaticRoute = function(static_file) {
return function(req, res) {
var mime_type = self.mimeType[static_file.substring(static_file.lastIndexOf('.') + 1)];
if (mime_type === undefined) {
mimeType = "text/plain";
}
res.setHeader('Content-Type', mime_type);
res.send(self.cache_get(static_file));
};
};
self.mimeType = { txt: "text/plain",
html: "text/html",
css: "text/css",
js: "text/javascript",
xml: "text/xml",
json: "application/json",
png: "image/png",
jpg: "image/jpeg",
jpeg: "image/jpeg",
gif: "image/gif"};
self.removeFileExtension = function (file_name) {
var slice = file_name.lastIndexOf('.');
if(slice > 0) {
return file_name.substring(0,slice);
}
return file_name;
};
self.createChat = function (chat_name) {
var chat = makeChat({server: true, chat_name: chat_name});
chat.systemMessage('Chat created', self.io);
return chat;
};
self.listenForConnections = function () {
self.io.sockets.on('connection', function (socket) {
socket.on('check if locked', function (data) {
console.log('checking if locked: ' + data.chat_url);
if (data.chat_url && self.chats[data.chat_url]) {
socket.chat_url = data.chat_url;
if (self.chats[data.chat_url].locked) {
socket.emit('chat locked');
socket.emit('new message', {text: 'Sorry, this chat is locked'});
}
else {
socket.emit('chat unlocked');
}
}
else {
console.log('bad request');
}
});
socket.on('join chat', function (data) {
if (data && data.chat_url && data.name) {
var chat = self.chats[data.chat_url];
if (chat) {
if (chat.locked) {
socket.emit('callback', 'join chat',
{accepted: false, error: 'Sorry, the chat has been locked'});
}
else if (chat.chatters.get(data.name)) {
socket.emit('callback', 'join chat',
{accepted: false, error: 'Sorry, the username ' + data.name + ' is already in use'});
}
else {
console.log(data.name + ' joined the chat');
socket.chatter = new chat.Chatter(data);
self.io.sockets.in(chat.chat_name).emit('new chatter', data);
socket.join(chat.chat_name);
socket.emit('initialize history', { chat_name: chat.chat_name,
chatters: chat.chatters,
messages: chat.messages});
socket.emit('callback', 'join chat', {accepted: true});
self.setupEvents(socket, chat);
}
}
else {
socket.emit('callback', 'join chat',
{accepted: false, error: 'Sorry, the chat no longer exists'});
}
}
else {
socket.emit('callback', 'join chat',
{accepted: false, error: 'Sorry, the join request was invalid'});
}
});
});
};
self.setupEvents = function (socket, chat) {
socket.on('new message', function (data) {
console.log('connections: ' + self.io.sockets.clients(chat.chat_name).length);
var message = new chat.Message(data);
self.io.sockets.in(chat.chat_name).emit('new message', data);
});
var disconnect = function() {
if (socket.chatter) {
var name = socket.chatter.name;
var message = new chat.Message({text: name + ' has left the chat'});
self.io.sockets.in(chat.chat_name).emit('new message', message);
chat.chatters.destroy(socket.chatter.name);
socket.leave(chat.chat_name);
self.io.sockets.in(chat.chat_name).emit('chatter disconnected', {name: name});
// reset chat if everyone has left
if (chat.chatters.length === 0 && socket.chat_url) {
self.chats[socket.chat_url] = undefined;
}
}
};
socket.on('disconnect', disconnect);
socket.on('leave chat', disconnect);
socket.on('clear messages', function() {
chat.messages = [];
self.io.sockets.in(chat.chat_name).emit('clear messages');
});
socket.on('lock chat', function() {
chat.locked = true;
self.io.sockets.in(chat.chat_name).emit('chat locked');
chat.systemMessage(socket.chatter.name + ' has locked the chat', self.io);
});
socket.on('unlock chat', function() {
chat.locked = false;
self.io.sockets.in(chat.chat_name).emit('chat unlocked');
chat.systemMessage(socket.chatter.name + ' has unlocked the chat', self.io);
});
};
/**
* Initialize the server (express) and create the routes and register
* the handlers.
*/
self.initializeServer = function() {
self.createRoutes();
self.app = express();
var svrOptions = {
key: fs.readFileSync('keys/ssl.key'),
cert: fs.readFileSync('keys/ssl.crt'),
ca: fs.readFileSync('keys/ca.unified.pem')
};
keys_dir = 'keys/'
self.server = require('https').createServer(svrOptions,self.app);
self.io = require('socket.io').listen(self.server);
self.chats = {};
self.chats['DaveChat'] = self.createChat('DaveChat');
self.listenForConnections();
// Add handlers for the app (from the routes).
for (var r in self.routes) {
self.app.get(r, self.routes[r]);
}
};
/**
* Initializes the application.
*/
self.initialize = function() {
self.setupVariables();
self.static_files = self.dirFiles('./static/');
//self.image_files = self.dirFiles('./images/');
self.populateCache();
self.setupTerminationHandlers();
setInterval(self.refreshCache, 1000);
// Create the express server and routes.
self.initializeServer();
};
/**
* Start the server (starts up the sample application).
*/
self.start = function() {
// Start the app on the specific interface (and port).
self.server.listen(self.port, self.ipaddress, function() {
console.log('%s: Node server started on %s:%d ...',
Date(Date.now() ), self.ipaddress, self.port);
});
};
};
var zapp = new ChatApp();
zapp.initialize();
zapp.start();