-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (93 loc) · 3.5 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
//Server
const WebSocket = require('ws');
const server = new WebSocket.Server({ host: '0.0.0.0', port: 5000 }, () => {
console.log(`${new Date().toLocaleString()} - Listening on port 5000...`);
});
server.on('connection', (ws, req) => {
new Player(ws, req.connection.remoteAddress);
ws.on('message', message => {
var objPlayer = Player.find(ws);
objPlayer.process(message);
});
ws.on('close', () => {
Player.remove(ws);
});
});
//Players
class Player {
constructor(socket, IPaddress) {
this.authenticated = false;
this.id = Player.globalID++;
this.IPaddress = IPaddress;
this.socket = socket;
Player.all.push(this);
console.log(`${new Date().toLocaleString()} - Player ${this.id} with IP address ${this.IPaddress} created`);
//Player needs to be authenticated within 2 seconds
this.authTimer = setTimeout(() => {
if (!this.authenticated) {
console.log(`${new Date().toLocaleString()} - Player ${this.id} authentication failed`);
this.socket.close();
}
},2000);
}
static find(socket) {
return(Player.all.find(x => x.socket === socket));
}
seeAll() {
for (var objPlayer of Player.all) {
if (objPlayer.id !== this.id) {
//Get details from all other players
this.socket.send(objPlayer.transformJSON());
}
};
}
process(message) {
var json = JSON.parse(message);
switch(json.command) {
case "username":
//Player is authenticated
this.username = json.data;
this.authenticated = true;
console.log(`${new Date().toLocaleString()} - Player ${this.id} authenticaton passed with username ${this.username}`);
this.socket.send('{"command":"auth","data":"true"}');
clearTimeout(this.authTimer);
this.seeAll();
break;
case "transform":
//Player moved
var transform = json.data;
this.x = transform.x;
this.y = transform.y;
this.z = transform.z;
this.rotation = transform.rotation;
this.sendToEveryoneElse(this.transformJSON());
break;
}
}
static remove(socket) {
//Tell all other players that we've gone
var objPlayer = Player.all.find(x => x.socket === socket);
var json = `{"command":"playerGone","data":${objPlayer.id}}`;
objPlayer.sendToEveryoneElse(json);
console.log(`${new Date().toLocaleString()} - Player ${objPlayer.id} gone`);
//Remove me from list of all players
Player.all = Player.all.filter((obj) => {
return obj.socket !== socket;
});
}
//Send message to all other players
sendToEveryoneElse(json) {
for (var objPlayer of Player.all) {
if (objPlayer.socket !== this.socket) {
objPlayer.socket.send(json);
}
};
}
//Position and rotation details
transformJSON() {
var json = `{"command":"playerMoved","data":{"id":${this.id},"username":"${this.username}","x":${this.x},"y":${this.y},"z":${this.z},"rotation":${this.rotation}}}`;
return json;
}
}
Player.all = new Array();
Player.globalID = 1;