-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
226 lines (184 loc) · 6.86 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
'use strict'
process.title = 'chatServer';
global.util = require('util');
var async = require('async');
require('./helpers/setUpLog.js');
require('./helpers/express/sendJSONResponse.js');
if (require.main === module) {
init();
}
process.on('uncaughtException',
function(err) {
throwError('Uncaught Exception thrown.', util.inspect(err));
});
function init(argument) {
// body...
var wallet = {
app: global.app,
config: {
runMode: 'dev',
apiPort: 8080
}
};
wallet.me = util.format('chatServer_%s', init.name);
async.series([
createExpressApp.bind(null, wallet),
initializeMongoose.bind(null, wallet),
startListening.bind(null, wallet),
initializeSockets.bind(null, wallet),
initializeRoutes.bind(null, wallet),
setLogLevel.bind(null, wallet)
],
function(err) {
if (err) {
log.error('Could not initialize api app: ' + util.inspect(err));
} else {
log.info(wallet.me, 'Completed');
global.app = wallet.app;
module.exports = global.app;
}
})
}
function createExpressApp(wallet, nextFunc) {
// body...
try {
var express = require('express')
var app = express()
app.use(require('body-parser').json());
app.use(require('body-parser').urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'));
var http = require('http')
wallet.app = app;
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
return nextFunc();
} catch (err) {
throwError('Error: Unable to create chatServer', err)
}
}
function startListening(wallet, nextFunc) {
var me = wallet.me + '_' + startListening.name;
log.debug('Executing ->', me);
var listenAddr = '0.0.0.0';
var apiPort = wallet.config.apiPort;
if (!apiPort)
return nextFunc(
console.log('error')
);
wallet.httpInstance = wallet.app.listen(apiPort, listenAddr,
function(err) {
if (err)
return nextFunc(err);
log.info('Server running on %s.', apiPort);
}
);
return nextFunc();
}
function initializeSockets(wallet, nextFunc) {
// body...
var io = require('socket.io').listen(wallet.httpInstance);
wallet.io = io;
global.io = wallet.io;
module.exports = global.io;
var rooms = require('./nf/getAllRooms.js')
wallet.rooms = rooms()
log.info(wallet.rooms)
console.log(wallet.rooms)
// usernames which are currently connected to the chat
var usernames = {};
// rooms which are currently available in chat
var rooms = ['room1', 'room2', 'room3'];
console.log(rooms)
io.sockets.on('connection', function(socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username, roomName) {
// store the username in the socket session for this client
socket.username = username;
// store the room name in the socket session for this client
socket.room = roomName;
// add the client's username to the global list
usernames[username] = username;
// send client to room 1
socket.join(roomName);
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected to ' + roomName);
// echo to room 1 that a person has connected to their room
socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room');
socket.emit('updaterooms', rooms, roomName);
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function(data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.in(socket.room).emit('updatechat', socket.username, data);
var chat = require('./nf/postChat.js');
chat({
room: socket.room,
from: socket.username,
message: data,
created_at: new Date()
});
});
socket.on('switchRoom', function(newroom) {
// leave the current room (stored in session)
socket.leave(socket.room);
// join new room, received as function parameter
socket.join(newroom);
socket.emit('updatechat', 'SERVER', 'you have connected to ' + newroom);
// sent message to OLD room
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username + ' has left this room');
// update socket session room title
socket.room = newroom;
socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username + ' has joined this room');
socket.emit('updaterooms', rooms, newroom);
});
// when the user disconnects.. perform this
socket.on('disconnect', function() {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
socket.leave(socket.room);
});
});
return nextFunc();
}
function initializeMongoose(wallet, nextFunc) {
// body...
var me = wallet.me + '_' + initializeMongoose.name;
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/chat');
return nextFunc();
}
function initializeRoutes(wallet, nextFunc) {
var me = wallet.me + '_' + initializeRoutes.name;
try {
require('./Routes.js')(wallet.app);
} catch (Exception) {
throwError('Error: Unable to create chatServer', err)
}
return nextFunc();
}
function setLogLevel(wallet, nextFunc) {
var me = wallet.me + '_' + setLogLevel.name;
// log.debug('Executing ->', me );
var logConfig = {};
logConfig.runMode = wallet.config.runMode;
log.debug('Setting log level as ' + logConfig.runMode);
log.configLevel(logConfig);
return nextFunc();
}
function throwError(whoAmIssage, err) {
log.error(whoAmIssage);
log.error(err);
if (err.stack) log.error(err.stack);
setTimeout(function() {
process.exit();
}, 3000);
}