-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (52 loc) · 1.46 KB
/
index.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
const { Server } = require("socket.io");
const io = new Server({
cors: {
origin: "*",
},
});
let players = [];
// global variables
// let persona;
io.on("connection", (socket) => {
console.log("user connected", socket.id);
// console.log(clientID);
// Give the newly connected socket
socket.on("data", (user) => {
console.log(user)
players.push(user)
io.emit("location", players)
})
// Messaging
socket.on("message", (mssgInfo) => {
console.log("Message from " + mssgInfo.username + ": " + mssgInfo.message);
io.emit("message", mssgInfo);
});
// Location
socket.on("location", (user) => {
// Find the player index instead of using findIndex
const player = players.find(p => p.username === user.username);
if (player) {
// Directly update the player object
player.x = user.x;
player.y = user.y;
player.z = user.z;
}
// Emit the full array instead of just player locations
io.emit("location", players);
});
socket.on("disconnect", (user) => {
const index = players.findIndex((p) => p.id === socket.id);
// Remove from the players array
console.log('Removed player', players[index]);
if(index !== -1) {
players.splice(index, 1);
}
// Update the locations for all players by emitting
io.emit('location', players)
});
});
// listening on localhost: 3000
let port = process.env.PORT || 3000;
io.listen(port, () => {
console.log("server online");
});