Skip to content
Snippets Groups Projects
Commit 37449ad1 authored by Patrick's avatar Patrick
Browse files

Added some code to handle duplicate users.

parent 6fe0e4e0
No related branches found
No related tags found
1 merge request!1Update 1
<<<<<<< HEAD
const express = require("express"); const express = require("express");
const {createServer} = require("http"); const {createServer} = require("http");
const {Server} = require("socket.io"); const {Server} = require("socket.io");
...@@ -11,20 +10,9 @@ app.use(require('cookie-parser')()); ...@@ -11,20 +10,9 @@ app.use(require('cookie-parser')());
const httpServer = createServer(app); const httpServer = createServer(app);
const io = new Server(httpServer); const io = new Server(httpServer);
=======
let express = require("express");
let { createServer } = require("http");
let { Server } = require("socket.io");
let app = express();
let httpServer = createServer(app);
let io = new Server(httpServer);
>>>>>>> bb7b9ca8915158bb546967322e6902ca4e8a04fa
// For some reason this doesn't read the public folder
app.use(express.static("public")); app.use(express.static("public"));
<<<<<<< HEAD
const port = 3000; const port = 3000;
const hostname = "localhost"; const hostname = "localhost";
...@@ -279,18 +267,11 @@ app.get("/game/:id", (req, res) => { ...@@ -279,18 +267,11 @@ app.get("/game/:id", (req, res) => {
return res.sendFile(__dirname + "/semipublic/game.html"); return res.sendFile(__dirname + "/semipublic/game.html");
}); });
}); });
=======
let port = 3000;
let hostname = "localhost";
>>>>>>> bb7b9ca8915158bb546967322e6902ca4e8a04fa
let connectedClients = [];
let clientCount = 0;
io.on("connection", (socket) => { io.on("connection", (socket) => {
let socketId = socket.id; let socketId = socket.id;
console.log("A new client has connected:", socketId); console.log("A new client has connected:", socketId);
<<<<<<< HEAD
const gameId = socket.handshake.query.gameId; const gameId = socket.handshake.query.gameId;
const token = socket.handshake.query.token; const token = socket.handshake.query.token;
const user = login.getUserForToken(token); const user = login.getUserForToken(token);
...@@ -327,46 +308,10 @@ io.on("connection", (socket) => { ...@@ -327,46 +308,10 @@ io.on("connection", (socket) => {
socket.on("disconnect", () => { socket.on("disconnect", () => {
if (game) { if (game) {
game.userLeave(socketId, user); game.userLeave(socketId, user);
=======
clientCount += 1;
connectedClients.push(socket);
socket.emit("hello", "2doku!"); // Emits "hello" event to the client and sends "2doku!"
socket.on("howdy", (arg) => { // Receives "Hello from 2doku!" from client when "howdy" event from the client is triggered
console.log(arg);
});
socket.on("test1", (arg) => {
console.log(arg);
connectedClients.forEach((socket) => {
// socket.emit("res1", "Update to all client from test1");
socket.emit("update", `Update to all client from client ${socketId} with button test1 pressed`);
});
});
socket.on("test2", (arg) => {
console.log(arg);
connectedClients.forEach((socket) => {
// socket.emit("res2", "Update to all client from test2");
socket.emit("update", `Update to all client from client ${socketId} with button test2 pressed`);
});
});
socket.on("disconnect", () => {
if (connectedClients.includes(socket)) {
let index = connectedClients.indexOf(socket);
connectedClients.splice(index, 1);
console.log(`Client ${socketId} has disconnected!`);
>>>>>>> bb7b9ca8915158bb546967322e6902ca4e8a04fa
} }
}); });
}); });
// Temporary solution to the public folder issue
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/game.html");
});
httpServer.listen(port, hostname, () => { httpServer.listen(port, hostname, () => {
console.log(`http://${hostname}:${port}`); console.log(`http://${hostname}:${port}`);
}); });
...@@ -9,12 +9,9 @@ ...@@ -9,12 +9,9 @@
"node-sudoku": "^1.0.0-beta.0", "node-sudoku": "^1.0.0-beta.0",
"socket.io": "^4.7.2", "socket.io": "^4.7.2",
"sql.js": "^1.8.0", "sql.js": "^1.8.0",
<<<<<<< HEAD
"sudoku": "^0.0.3", "sudoku": "^0.0.3",
"sudoku-gen": "^1.0.2", "sudoku-gen": "^1.0.2",
"sudokutoolcollection": "^1.1.3", "sudokutoolcollection": "^1.1.3",
=======
>>>>>>> bb7b9ca8915158bb546967322e6902ca4e8a04fa
"ws": "^8.13.0" "ws": "^8.13.0"
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment