Made multiplayer support semi working
The server is unstable and crashes often
This commit is contained in:
parent
1b52ac9f7b
commit
e0baa9cd3f
5
package-lock.json
generated
5
package-lock.json
generated
@ -498,6 +498,11 @@
|
||||
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
|
||||
"dev": true
|
||||
},
|
||||
"colors": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
|
||||
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
|
||||
},
|
||||
"component-bind": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz",
|
||||
|
@ -28,6 +28,7 @@
|
||||
},
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"express": "^4.17.1",
|
||||
"socket.io": "^2.3.0"
|
||||
},
|
||||
|
54
src/index.js
54
src/index.js
@ -2,6 +2,21 @@ var app = require("express")();
|
||||
var http = require("http").createServer(app);
|
||||
var io = require("socket.io")(http);
|
||||
|
||||
var colours = require("colors");
|
||||
colours.enable();
|
||||
colours.setTheme({
|
||||
silly: "rainbow",
|
||||
input: "grey",
|
||||
verbose: "cyan",
|
||||
prompt: "grey",
|
||||
info: "green",
|
||||
data: "grey",
|
||||
help: "cyan",
|
||||
warn: "yellow",
|
||||
debug: "blue",
|
||||
error: "red"
|
||||
});
|
||||
|
||||
var path = require("path");
|
||||
|
||||
// Send html & css files
|
||||
@ -13,15 +28,25 @@ app.get("/index.css", (req, res) => {
|
||||
res.sendFile(path.join(__dirname + "/../css/index.css"));
|
||||
});
|
||||
|
||||
var players = [];
|
||||
var players = {};
|
||||
|
||||
// Handle Connection
|
||||
io.on("connection", (socket) => {
|
||||
// Handle disconnection
|
||||
socket.on("disconnect", () => {
|
||||
delete players[String(socket.id)];
|
||||
console.warn(players);
|
||||
console.log(`User ${socket.id} disconnected!`);
|
||||
const room = players[socket.id].room;
|
||||
delete players[socket.id];
|
||||
|
||||
var playerUpdate = {};
|
||||
if (io.sockets.adapter.rooms[room]) {
|
||||
for (const id in io.sockets.adapter.rooms[room].sockets) {
|
||||
playerUpdate[id] = players[id];
|
||||
}
|
||||
}
|
||||
|
||||
io.in(room).emit("roomUpdate", playerUpdate);
|
||||
|
||||
console.log(colours.warn(`User ${String(socket.id)} disconnected from room ${room}`));
|
||||
});
|
||||
|
||||
// Request identification
|
||||
@ -41,21 +66,26 @@ io.on("connection", (socket) => {
|
||||
|
||||
socket.join(room);
|
||||
|
||||
players[String(socket.id)] = {
|
||||
players[socket.id] = {
|
||||
username: data.name,
|
||||
controls: data.controls,
|
||||
room: room
|
||||
};
|
||||
console.warn(players);
|
||||
|
||||
io.in(room).emit("roomUpdate", players);
|
||||
var playerUpdate = {};
|
||||
for (const id in io.sockets.adapter.rooms[room].sockets) {
|
||||
playerUpdate[id] = players[id];
|
||||
}
|
||||
|
||||
console.log(`User ${socket.id} connected to room ${room}`);
|
||||
io.in(room).emit("roomUpdate", playerUpdate);
|
||||
|
||||
console.log(colours.help(`User ${String(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),
|
||||
if (players[socket.id]) {
|
||||
socket.to(players[socket.id].room).emit("update", {
|
||||
uuid: socket.id,
|
||||
controls: data
|
||||
});
|
||||
}
|
||||
@ -63,5 +93,5 @@ io.on("connection", (socket) => {
|
||||
});
|
||||
|
||||
http.listen(3000, () => {
|
||||
console.log("listening on *:3000");
|
||||
console.log(colours.data("\nTanksJS-Server ready, listening on port 3000\n"));
|
||||
});
|
Loading…
Reference in New Issue
Block a user