-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
44 lines (34 loc) · 1.31 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
let net = require('net');
let commandHandler = require('./commandHandler');
let utils = require('./utils');
let clients = {};
net.createServer(socket => {
// welcome user to the chat
socket.write("Welcome to the chat!\n");
// initially set socket nickname to ip:port
socket.nickName = socket.remoteAddress + ':' + socket.remotePort;
// push client to the clientsMap
clients[socket.nickName] = socket;
// broadcast new connection
utils.broadcast(socket.nickName + " joined the chat.\n", socket, clients);
let text;
// handle incoming data
socket.on('data', message => {
text = message.toString('utf8');
text = text.trim();
// check if client is trying to execute a command
if (text[0] == '/') {
clients = commandHandler(text.substring(1, text.length), socket, clients);
}
else {
// if no command is being executed, broadcast message
utils.broadcast(socket.nickName + "> " + message, socket, clients);
}
});
// remove socket from clients when connection ends
socket.on('end', () => {
broadcast(socket.nickName + ' left the chat.\n', null, clients);
delete clients[socket.nickName];
});
}).listen(5000);
console.log('chat server listening on localhost:5000');