-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (141 loc) · 3.9 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
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
const WebSocket = require('ws');
const uuidv1 = require('uuid/v1');
const wss = new WebSocket.Server({
port: 8080,
perMessageDeflate: {
zlibDeflateOptions: { // See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3,
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
clientMaxWindowBits: 10, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024, // Size (in bytes) below which messages
// should not be compressed.
}
});
var clients = [];
wss.on('connection', function connection(ws, req) {
var sessionId = req.headers['sec-websocket-key'];
console.log('New connection id :: ', sessionId);
ws.on('message', function incoming(message) {
console.log('received: %s', message);
var msg = JSON.parse(message);
switch (msg.cmd) {
case 'request-login':
clients.push({
sessionId: sessionId,
username: msg.username,
socket: ws,
});
var data = {
cmd: 'login-suceeded',
sessionId: sessionId,
};
ws.send(JSON.stringify(data));
sendBroadcast(sessionId, {
cmd: 'someone-entered',
sessionId: sessionId,
username: msg.username,
});
break;
case 'request-logout':
logout(ws);
break;
case 'request-rename':
if (clients[msg.sessionId]) {
clients[msg.sessionId].username = msg.username;
sendBroadcast(msg.sessionId, {
cmd: 'someone-renamed',
sessionId: msg.sessionId,
username: msg.username,
});
}
break;
case 'fetch-clients':
var data = {
cmd: 'clients-arrived',
clients: getClients(msg.sessionId),
};
ws.send(JSON.stringify(data));
break;
case 'send-text':
var data = {
cmd: 'send-text',
receiver: msg.receiver,
text: msg.text,
time: Date.now(),
};
ws.send(JSON.stringify(data));
sendText(msg.sessionId, msg.receiver, msg.text, data.time);
break;
}
});
ws.on('error', function error(err) {
console.log('error occurred :: ', err.message);
});
ws.on('close', function close(code, reason) {
console.log('disconnected :: ', reason);
logout(ws);
});
// ws.send('something');
});
function getClients(excluder) {
var result = [];
for (var i = 0; i < clients.length; i++) {
if (clients[i].sessionId == excluder)
continue;
result.push({
sessionId: clients[i].sessionId,
username: clients[i].username,
});
}
return result;
}
function sendBroadcast(sender, data) {
for (var i = 0; i < clients.length; i++) {
if (clients[i].sessionId == sender)
continue;
clients[i].socket.send(JSON.stringify(data));
}
}
function sendText(sender, receiver, text, time) {
for (var i = 0; i < clients.length; i++) {
if (clients[i].sessionId == receiver) {
var data = {
cmd: 'receive-text',
sender: sender,
text: text,
time: time,
};
clients[i].socket.send(JSON.stringify(data));
return true;
}
}
return false;
}
function logout(ws) {
var sender = getSessionId(ws);
if (sender !== false) {
sendBroadcast(sender, {
cmd: 'someone-left',
sessionId: sender,
});
delete clients[sender];
}
}
function getSessionId(ws) {
for (var i = 0; i < clients.length; i++) {
if (clients[i].socket == ws)
return clients[i].sessionId;
}
return false;
}