forked from arne/TanksJS-Server
[wip] Added basic client - server communication
Encountering issues with socket id's in players array
This commit is contained in:
parent
0439c5ff6c
commit
1b52ac9f7b
48
src/index.js
48
src/index.js
@ -13,9 +13,53 @@ app.get("/index.css", (req, res) => {
|
|||||||
res.sendFile(path.join(__dirname + "/../css/index.css"));
|
res.sendFile(path.join(__dirname + "/../css/index.css"));
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
var players = [];
|
||||||
|
|
||||||
|
// Handle Connection
|
||||||
io.on("connection", (socket) => {
|
io.on("connection", (socket) => {
|
||||||
console.log("A user connected");
|
// Handle disconnection
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
delete players[String(socket.id)];
|
||||||
|
console.warn(players);
|
||||||
|
console.log(`User ${socket.id} disconnected!`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Request identification
|
||||||
|
socket.emit("identify");
|
||||||
|
|
||||||
|
// Wait for identification
|
||||||
|
socket.on("identification", (data) => {
|
||||||
|
var room;
|
||||||
|
switch (data.gamemode) {
|
||||||
|
case 2:
|
||||||
|
room = "2_01";
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
room = "4_01";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.join(room);
|
||||||
|
|
||||||
|
players[String(socket.id)] = {
|
||||||
|
username: data.name,
|
||||||
|
room: room
|
||||||
|
};
|
||||||
|
console.warn(players);
|
||||||
|
|
||||||
|
io.in(room).emit("roomUpdate", players);
|
||||||
|
|
||||||
|
console.log(`User ${socket.id} connected to room ${room}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("update", (data) => {
|
||||||
|
if (players[String(socket.id)]) {
|
||||||
|
socket.to(players[String(socket.id)].room).emit("update", {
|
||||||
|
uuid: String(socket.id),
|
||||||
|
controls: data
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
http.listen(3000, () => {
|
http.listen(3000, () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user