-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
59 lines (44 loc) · 1.36 KB
/
app.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
var express = require('express');
var redis = require('redis');
var init = require('./init');
var routes = require('./routes/index');
var client = redis.createClient();
var client2 = redis.createClient();
// client.auth(YOUR_PASSWORD);
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
init.createApp(app);
app.use('/', routes);
init.enableErrorHandlers(app);
// REDIS queue
client.publish('instances', 'start');
client2.on('message', function (channel, message) {
if ((channel == 'instances') && (message == 'start'))
console.log('New instance started!');
else if ((channel == 'instances') && (message == 'stop'))
console.log('Instance stopped!');
else if (channel == 'chat') {
console.log('Received chat message:'+message);
io.emit('chat message', message);
}
});
client2.subscribe('instances');
client2.subscribe('chat');
process.on('SIGTERM', function () {
client.publish('instances', 'stop');
process.exit();
});
// socket.io
io.on('connection', function(socket){
socket.on('chat message', function(msg){
client.publish('chat', msg);
console.log('Received chat message:'+msg);
});
});
var port = process.env.PORT || '3000';
http.listen(port, function(){
console.log('listening on *:'+port);
});
module.exports.app = app;
module.exports.server = http;