-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (67 loc) · 1.62 KB
/
index.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
const net = require('net');
var count = 0
var users = {}
net.createServer((conn) => {
var grammar;
if (count < 1){
grammar = " persons connected at this time";
}
else if (count === 1){
grammar = " other person connected"
}
else{
grammar = " other people connected"
}
conn.write("\n > welcome to chat"
+ "\n > " + count + `${grammar}.`
+ "\n > please write your name and press enter: ");
count ++
var nickname;
//sets encoding for data to be transmitted. Originally appears as just Buffer
conn.setEncoding('utf-8')
//Client departure alert
function broadcast (msg) {
for (var i in users) {
if (i != nickname) {
users[i].write(msg);
}
}
}
console.log("new connection")
//data event listener
conn.on('data', function (data) {
data = data.replace('\r\n', '');
// console.log(data)
//expect first data to be nickname
if (!nickname) {
if (users[data]) {
conn.write("nickname already in use. try again:");
return;
} else {
nickname = data;
users[nickname] = conn;
for (var i in users) {
users[i].write("\n"+nickname + " joined the room\n");
}
}
}
else {
// otherwise consider it a chat message
//broadcast it to everyone that is not me.
for (var i in users) {
if (i != nickname) {
users[i].write("\n" + nickname + ": " + data + "\n");
}
}
}
});
conn.on('close', () => {
if (nickname != undefined){
broadcast("\n"+nickname + "left the room\n")
count--
delete users[nickname]
}
});
}).listen(3000, () => {
console.log("Server listening on port 3000");
});