This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
143 lines (134 loc) · 5.19 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
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
const ws = require('ws');
const uuid = require('uuid');
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
function containsUser(ws) {
if (!gnetServer.servers[ws.targetServer]) return false;
let retval = false;
gnetServer.servers[ws.targetServer].forEach(user => {
if (retval) return;
if (user.userID == ws.userID) retval = true;
});
return retval;
}
function addServer(srv) {
if (gnetServer.servers[srv]) return;
gnetServer.servers[srv] = [];
gnetServer.servers[srv].host = () => {
for (const client of gnetServer.servers[srv]) {
if (client.host) return client;
}
return undefined;
};
gnetServer.servers[srv].findClient = (userID) => {
for (const client of gnetServer.servers[srv]) {
if (client.userID == userID) return client;
}
return undefined;
};
}
const gnetServer = new ws.Server({ noServer: true });
gnetServer.servers = {};
gnetServer.on('connection', (ws, req) => {
ws.packet=(p)=>{ws.send(JSON.stringify(p))}
ws.on('message', (msg) => {
let packet = JSON.parse(msg);
if (packet.type == 'control/userid') { // When the client responds with a userID control packet
if (ws.userID || ws.connected) return;
ws.userID = packet.data.userID;
if ((gnetServer.servers[ws.targetServer] && containsUser(ws)) || ws.userID == "[{broadcast}]") { // If the userID is already in use or it is broadcast
return ws.packet({ // we notify the client with a {valid: false} parameter
type: 'control/userid', data: {
valid: false,
userID: ws.userID
}
});
}
ws.connected = true; // We connect the client and add them to the server
addServer(ws.targetServer);
ws.host = gnetServer.servers[ws.targetServer].length <= 0;
if (!ws.host) {
// Notify the host about the new connection
gnetServer.servers[ws.targetServer].host().packet({
type: 'control/connection',
data: {
userID: ws.userID
}
});
}
gnetServer.servers[ws.targetServer].push(ws);
ws.packet({ // Validate the handshake
type: 'control/userid', data: {
valid: true,
userID: ws.userID
}
});
ws.packet({ // Send the mode select packet
type: 'control/mode', data: {
host: ws.host
}
});
} else {
if (ws.host && packet.destination) {
if (packet.destination == "[{broadcast}]") { // broadcast the packet to all clients
gnetServer.servers[ws.targetServer].forEach(client => client.packet({
type: 'host/packet',
data: packet.data
}));
} else {
try { // try to send the packet to the destination client
gnetServer.servers[ws.targetServer].findClient(packet.destination).packet({
type: 'host/packet',
data: packet.data
});
} catch (e) {}
}
} else {
// Client sending data to host
gnetServer.servers[ws.targetServer].host().packet({
type: 'client/packet',
source: ws.userID,
data: packet
});
}
}
});
ws.on('close', (code, reason) => {
if (!ws.connected) return;
gnetServer.servers[ws.targetServer].remove(ws); // Remove the client from the server
if (ws.host && gnetServer.servers[ws.targetServer].length > 0) {
// If the user is a host we need to select a new host and notify them
gnetServer.servers[ws.targetServer][0].host = true;
gnetServer.servers[ws.targetServer][0].packet({ // Send the mode select packet again
type: 'control/mode', data: {
host: true
}
});
}
// Notify the host about the disconnection
if (gnetServer.servers[ws.targetServer].length > 0)
gnetServer.servers[ws.targetServer].host().packet({
type: 'control/disconnection',
data: {
userID: ws.userID
}
});
// Delete the server if empty
if (gnetServer.servers[ws.targetServer].length <= 0) delete gnetServer.servers[ws.targetServer];
});
ws.targetServer = req.url.split("/")[2]; // We grab the server from the URL
ws.packet({ type: 'control/handshake', data: { // Start the handshake
target: ws.targetServer
} });
});
module.exports = {
gnetServer: gnetServer
};