-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (67 loc) · 2.03 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
const WebSocketServer = require('ws').Server,
wss = new WebSocketServer({ port: 8181 });
var clientID = 0;
var clients = {};
var defaultMessages = ['Congratulations! You have connected successfully.'];
/* The handshake has been completed */
wss.on('connection', (ws)=> {
var nickName = 'Anonymous' + clientID;
clients[nickName] = ws;
var notification = {
type: 'notification',
data: [nickName + ' has just joined this room.']
};
wss.clients.forEach(function each(client) {
if (client !== ws) {
client.send(JSON.stringify(notification));
}
});
clientID++;
console.log('[Info]' + nickName + ' has connected.');
ws.on('message', (message) => {
var request = JSON.parse(message);
switch(request.type) {
case 'fetchDefault':
ws.send(JSON.stringify({
type: 'message',
data: defaultMessages
}));
break;
case 'createMessage':
var broadcastData = {
type: 'message',
data: [ nickName + ': ' + request.data]
}
wss.clients.forEach(function each(client) {
client.send(JSON.stringify(broadcastData));
});
break;
}
});
ws.on('close', () => {
console.log('[Notification]', nickName + ' has disconnected');
wss.clients.forEach((client) => {
client.send(JSON.stringify({
type: 'notification',
data: [nickName + ' has disconnected']
}));
});
});
ws.on('error', (error) => {
console.error('[Error]', 'The client ' + nickName + ' occurred error:', error);
});
});
/* An error occurs on the underlying server */
wss.on('error', (error) => {
console.error('[Error] An error occurred on the underlying server.', error);
});
/* The server has been bound */
wss.on('listening', () => {
console.log('[Info]The server is listening.');
});
/**
* Emitted before the response headers are written to the socket as part of the handshake.
* This allows you to inspect/modify the headers before they are sent.
*/
wss.on('headers', (headers) => {
});