-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupChatServer.js
87 lines (70 loc) · 2.16 KB
/
groupChatServer.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
/*
* A (very) simple Group chat server
*
*/
/*
* Local interface address from which server listens for users.
* change it to suite your machine.
*/
const SERVER_HOST = "127.0.0.1";
/*
* Port number on which server listens for users.
* change it to which ever you want
*/
const SERVER_PORT = 8765;
const net = require('net');
// Maximum number of users that can be online at any time
const MAXIMUM_ONLINE_USERS = 100
// list of online users
userList = []
// create a TCP socket
serverSock = net.createServer(clientConnected);
// catch any socket error
serverSock.on('error', function(err) {
if (err.code === 'EADDRINUSE') {
console.log("Error: port %d is in use, check if another instance of the server is running or wait for a few seconds and rerun the server", SERVER_PORT);
} else {
console.log("server socket error:", err.code);
}
});
/* listen for the user connections */
serverSock.listen(SERVER_PORT, SERVER_HOST, function() {
console.log('Server listening on %s:%d', SERVER_HOST, SERVER_PORT);
});
/* User messages handling functions */
function clientConnected(clientSocket) {
if (userList.length >= MAXIMUM_ONLINE_USERS) {
clientSocket.end();
}
clientSocket.on('error', function(err) {
clientSocket.destroy(err);
});
clientSocket.on('data', function(buffer) {
/* broadcast the message to all other users */
broadcastMessage(clientSocket, buffer);
});
/* user closes the connection, remoe the user from the list*/
clientSocket.on('end', function() {
/* remove the user from the list */
userList.splice(userList.indexOf(clientSocket), 1);
});
clientSocket.on('close', function(err) {
if (err) {
console.log('Connection closed due to socket error');
}
});
/* add the user to the list */
userList.push(clientSocket);
}
/*broadcast user message to all other online users */
function broadcastMessage(from, message) {
if (userList.length === 0) {
return;
}
userList.forEach(function(userSocket) {
if (userSocket === from) {
return;
}
userSocket.write(message);
});
}