-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (175 loc) · 6.92 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
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
var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var net = require('net');
var globals = require('./global');
var message = require('./model/message');
var notificationUtils = require('./lib/notificationUtils');
var connectionUtils = require('./lib/connectionUtils.js');
var sticky = require('sticky-session');
var os = require('os');
console.log(globals.project_root);
console.log(process.argv);
globals.instance = {
name: process.argv[2]
}
var app = new express();
app.enable('trust proxy');
//ENABLE CORS
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
var options = {
key: fs.readFileSync('ssl/notiier.key'),
cert: fs.readFileSync('ssl/notiier.wildcard.chained.crt')
};
var server;
if (globals.useSSL) {
server = https.createServer(options, app);
} else {
server = http.createServer(app);
}
var port = globals.workers.start_port + parseInt(process.argv[3]);
server.listen(port, function(err) { // port
if (err) {
console.log("[" + globals.mode +"] Error while starting server.", err);
}
console.log("[" + globals.mode +"] Instance started on port", port); // port
});
var sockets = require('socket.io')();
var io = sockets.listen(server,{ resource: '/socket.io/','sync disconnect on unload':true });
io.set('origins', '*:*');
io.set('transports', ['websocket']);
console.log ("SocketIO Version: " + require('socket.io/package').version);
globals.io = io;
// Tell Socket.IO to use the redis adapter. By default, the redis
// server is assumed to be on localhost:6379. You don't have to
// specify them explicitly unless you want to change them.
var redisSIO = require('socket.io-redis');
if(globals.mode==='DEVELOPMENT') {
io.adapter(redisSIO({host: globals.redis.host, port: globals.redis.port}));
io.of('/').adapter.on('error', function () {
console.log("Error while initializing socket.io-redis adapter.");
});
} else if(globals.mode==='PRODUCTION') {
var client = require('redis').createClient;
var pub = client({url: globals.redis.connection_url, return_buffers: true});
var sub = client({url: globals.redis.connection_url, return_buffers: true});
io.adapter(redisSIO({ pubClient: pub, subClient: sub }));
pub.on("ready", function(){
console.log('redis pub - ready');
});
pub.on("connect", function(){
globals.redis.connected = true;
console.log("redis pub - connect");
});
pub.on("error", function(){
console.log("redis pub - error");
});
sub.on("ready", function(){
console.log('redis sub - ready');
});
module.exports = app;
module.exports = globals;
}
/********************************** routes **************************************/
var index = require('./routes/index');
var connections = require('./routes/connections');
var notify = require('./routes/notify');
var status = require('./routes/status');
var test = require('./routes/test');
app.use('/notiier', index);
app.use('/notiier/connections', connections);
app.use('/notiier/notify', notify);
app.use('/notiier/status', status);
app.use('/notiier/test', test);
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
res.json(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.json(err);
//res.render('error');
//next();
});
module.exports = app;
module.exports = globals;
/*
- Uncomment it if you want to send heartbeat constantly.
io.set('heartbeat timeout', 4000);
io.set('heartbeat interval', 5000);
*/
io.on ('connection', function (socket) {
socket.on ('disconnect', function(e) {
console.log("[DELETED] connection deleted successfully, socket id : " + socket.id);
});
socket.on ('join', function (data) {
var clientIp = socket.request.connection.remoteAddress, sHeaders = socket.handshake.headers;
var orgToken = data.org_token, cookieId = data.cookie_id;
var userAgent = "";
if (sHeaders.hasOwnProperty ('user-agent')) {
userAgent = sHeaders['user-agent'];
}
console.log("[JOINED]", JSON.stringify({
orgToken: orgToken,
cookieId: cookieId,
socketId: socket.id,
clientIp: clientIp,
"x-forwarded-for": sHeaders['x-forwarded-for'],
instance: process.argv[2]
}));
/*console.log ("[JOINED] Cookie: " + cookieId + " | org: " + orgToken + " (" + socket.id + " @ " + sHeaders['x-forwarded-for']
+ ") on instance " + process.argv[2] + ", Remote address - " + clientIp);*/
socket.join(orgToken); // join room, room name is <org_token>, meant for sending global(*) message.
socket.join(cookieId); // join room, room name is <cookie_id>. meant for sending personal/individual message.
// Send pending messages.
notificationUtils.sendPendingMessages(orgToken, cookieId);
});
// Acknowledgement sent by client once message is received.
socket.on('acknowledge', function(data) {
if(data.hasOwnProperty("org_token") && data.hasOwnProperty("cookie_id") && data.hasOwnProperty("msg_id")) {
var orgToken = data['org_token'];
var cookieId = data['cookie_id'];
var msgId = data['msg_id'];
for(var i in data.cookie_ids) {
var cid = data.cookie_ids[i];
if(cid=='*') {
message.markMessageAsRead(cookieId, msgId, data.ttl);
} else if(cid==cookieId){
// Personal messages will be removed from notice board, once target user read them successfully.
// Delete user's individual message from notice board. (Letter box)
message.removeMessageFromNoticeBoard(orgToken, cookieId);
}
}
console.log("Message - " + data.msg_id + " has been read by user - " + data.cookie_id);
}
});
socket.on ('error', function (err) {
console.error (err.stack);
});
});