-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (64 loc) · 1.77 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
import { WebSocketServer } from 'ws';
class sockets {
#socket;#group;#nickname;
constructor(socket, nick, group) {
this.setSocket=socket
this.setGroup=group
this.setNick=nick
}
set setNick(nickname) {
this.#nickname=nickname;
}
get nick() {
return this.#nickname
}
set setSocket(socket) {
this.#socket=socket;
}
set setGroup(group) {
this.#group=group;
}
get socket() {
return this.#socket;
}
get group() {
return this.#group
}
}
var webSockets = [];
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.send('connection successful');
ws.on('message', function message(data) {
var message=JSON.parse(data.toString())
if(message.connect == 1) {
console.log("CONNESSO")
for(let i=0; i<webSockets.length; i++) {
if(webSockets[i].socket!=ws) {
webSockets.push(new sockets(ws, message.nick, message.group))
console.log("ID: "+i+" NICK: "+webSockets[i].nick+" GROUP: "+webSockets[i].group)
}
if(webSockets[i].socket==ws) {
webSockets[i].setSocket=ws;
webSockets[i].setGroup=message.group;
webSockets[i].setNick=message.nick;
console.log("ID: "+i+" NICK: "+webSockets[i].nick+" GROUP: "+webSockets[i].group)
}
}
}
else {
console.log('received: %s', message.msg);
let group;
for(let j=0; j<webSockets.length; j++) {
if(webSockets[j].socket==ws) {
group=webSockets[j].group;
}
}
for(let j=0; j<webSockets.length; j++) {
if(webSockets[j].socket!=ws && webSockets[j].group == group) {
webSockets[j].socket.send(message.msg.toString());
}
}
}
});
});