-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket_server.js
More file actions
134 lines (118 loc) · 4.43 KB
/
websocket_server.js
File metadata and controls
134 lines (118 loc) · 4.43 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
129
130
131
132
133
134
const fs = require('fs');
const https = require('https');
const ws = require('ws');
// Read SSL certificate and private key
const privateKey = fs.readFileSync('./certificates/server.key', 'utf8');
const certificate = fs.readFileSync('./certificates/server.cert', 'utf8');
const credentials = {
key: privateKey,
cert: certificate
};
const args = process.argv;
// Create an HTTPS server with the SSL credentials
const httpsServer = https.createServer(credentials);
httpsServer.listen(args[3], args[2], () => {
console.log("Starting WebSocket Server at " + args[2] + ":" + args[3]);
});
// Create the WebSocket server on top of the HTTPS server
const wss = new ws.Server({ server: httpsServer });
//all connected to the server users
var users = new Map();
var currNumPlayers = new Map();
var userNames = new Set();
var userGroups = new Map();
var userGroupsId = 0;
//number of players to connect
var totalNumPlayers = 0;
wss.on('connection', function(connection) {
connection.on('message', function(message) {
var data = message;
if(isValidJSON(data) == false)
{
const length = data.length;
const name = data.readInt8(length - 2); // 0
const group = data.readUInt8(length - 1); // 1
var conn = users[group][name];
data.writeInt8(connection.name.split('/')[1], length - 2); // 0
conn.send(data);
}
else
{
var data = JSON.parse(message);
console.log("Player: " + data.name + " group: " + data.group);
//if anyone is logged in with this username then refuse
if(userGroups.has(data.group) && (typeof users[userGroups.get(data.group)][data.name] !== 'undefined')) {
sendTo(connection, {
type: "login",
success: false,
message: "Username is unavailable!"
});
} else if((userNames.size != 0) && (totalNumPlayers != data.content)) {
console.log("Number of players " + data.content + " does not match with other players.");
console.log("Current number of players: " + userNames.size);
sendTo(connection, {
type: "login",
success: false,
message: "Number of players does not match!"
});
} else {
// first player sets the number of players
if(userNames.size == 0)
totalNumPlayers = data.content;
if(!userGroups.has(data.group))
userGroups.set(data.group, userGroupsId++);
let groupid = userGroups.get(data.group);
// save user connection on the server
if(currNumPlayers[groupid] == null) {
currNumPlayers[groupid] = 0;
users[groupid] = Array.apply(null, Array(totalNumPlayers)).map(function () {});
}
users[groupid][data.name] = connection;
userNames.add(data.name);
connection.name = groupid + '/' + data.name;
currNumPlayers[groupid]++;
// notify all players when all players have logged in
if(currNumPlayers[groupid] == totalNumPlayers) {
console.log("All players have logged in");
for(let i of users[groupid]) {
sendTo(i, {
type: "login",
success: true,
groupid: groupid
});
}
}
}
}
});
// when user exits/closes the browser window or established webrtc connections
connection.on("close", function() {
if(connection.name) {
var group = connection.name.split('/')[0];
var name = connection.name.split('/')[1];
users[group][name] = undefined;
currNumPlayers[group]--;
if(currNumPlayers[group] == 0) {
userGroups.delete(group);
users.delete(group);
}
console.log("Disconnected from", connection.name);
}
if(!userGroups || userGroups.size == 0) {
console.log("All players have disconnected");
userNames.clear();
// setTimeout(shutDown, 3000); // wait 3 seconds before shutting down server
}
});
});
function sendTo(connection, message) {
connection.send(JSON.stringify(message));
}
function isValidJSON(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}