-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
128 lines (112 loc) · 3.3 KB
/
Copy pathserver.js
File metadata and controls
128 lines (112 loc) · 3.3 KB
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
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
/* global require */
/* global console */
(function WC() {
const wsServer = require('ws');
const self = this;
const webSocketServer = new wsServer.Server({
port: 8090,
});
function WebChat() {
this.defaultRoom = 'Self';
this.roomsList = new Map();
this.connections = {};
}
WebChat.prototype = {
getRoomsList: function getRoomsList(clientId) {
const roomsListRes = [];
this.roomsList.forEach((v, k) => {
if (!!~v.participants.indexOf(this.getRemote(clientId))) {
roomsListRes.push({ name: k });
}
});
return roomsListRes;
},
joinToRoom: function joinToRoom(roomName, remoteUsername) {
if (!this.roomsList.has(roomName)) {
this.roomsList.set(roomName, {
participants: [remoteUsername],
});
} else {
this.roomsList.get(roomName).participants.push(remoteUsername);
}
},
setRemote: function setRemote(clientId, remoteUsername) {
Object.keys(this.connections).forEach((id) => {
if (parseFloat(id) !== clientId) {
if (this.connections[id].remoteUser === remoteUsername) {
this.connections[id].socket.close();
delete this.connections[id];
}
}
});
this.connections[clientId].remoteUser = remoteUsername;
// this should be done in 'register' function when DB is available
this.joinToRoom(this.defaultRoom, remoteUsername);
},
getRemote: function getRemote(clientId) {
return this.connections[clientId].remoteUser;
},
sendMessage: function sendMessage(clientId, message) {
this.connections[clientId].socket.send(JSON.stringify(message));
},
handleMessage: function handleMessage(clientId, message) {
// console.log(message);
switch (message.cmd) {
case 'getRoomsList':
this.sendMessage(clientId,
{
cmd: 'getRoomsList',
roomsList: this.getRoomsList(clientId),
}
);
break;
case 'getMessagesList':
this.sendMessage(clientId,
{
cmd: 'getMessagesList',
messagesList: [{ text: 'AAA' }, { text: 'BBB' }],
}
);
break;
case 'newMessage':
this.sendMessage(clientId,
{
cmd: 'newMessage',
result: 'OK',
}
);
break;
case 'register':
this.setRemote(clientId, message.opts.remote);
this.sendMessage(clientId,
{
cmd: 'register',
result: 'OK',
}
);
break;
default:
console.log(`Unexpected command! ${message.cmd}`);
break;
}
},
};
webSocketServer.on('connection', (ws) => {
const clientId = Math.random();
self.WC.connections[clientId] = {};
self.WC.connections[clientId].socket = ws;
ws.on('message', (message) => {
try {
self.WC.handleMessage(clientId, JSON.parse(message));
} catch (e) {
console.log(e);
}
});
ws.on('close', () => {
delete self.WC.connections[clientId];
console.log('disconnected');
});
});
global.WC = new WebChat();
}());