Skip to content

Commit

Permalink
Merge pull request #11 from shammy642/setting-up-socket-server
Browse files Browse the repository at this point in the history
Setting up socket server
  • Loading branch information
shammy642 authored Oct 16, 2024
2 parents 747f2be + 3cc3928 commit 82d7b50
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 5 deletions.
31 changes: 27 additions & 4 deletions api/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const gamesRouter = require('./routes/games')
const tokenChecker = require("./middleware/tokenChecker");
const { Server } = require("socket.io")
const http = require('http')
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const gamesRouter = require('./routes/games')
const tokenChecker = require("./middleware/tokenChecker");


const app = express();
Expand All @@ -18,7 +20,6 @@ app.use(bodyParser.json());
// API Routes
app.use('/game', tokenChecker, gamesRouter)


// 404 Handler
app.use((_req, res) => {
res.status(404).json({ err: "Error 404: Not Found" });
Expand All @@ -34,4 +35,26 @@ app.use((err, _req, res, _next) => {
}
});

module.exports = app;
const server = http.createServer(app)

const io = new Server(server, {
cors: {
origin: "http://localhost:5173"
}
})

io.on('connection', (socket) => {
console.log(`User Connected: ${socket.id}`)
console.log(`Users Connected: ${io.engine.clientsCount}`)

socket.on('disconnect', () => {
console.log("User disconnected!")
console.log(`Users Connected: ${io.engine.clientsCount}`)
})
})

server.listen(3001, () => {
console.log("Server running at http://localhost:3001");
});

module.exports = app;
1 change: 1 addition & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require("dotenv").config();
const app = require("./app.js");



function listenForRequests() {
const port = process.env.PORT || 3000;
app.listen(port, () => {
Expand Down
143 changes: 143 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
"devDependencies": {
"@types/express": "^5.0.0",
"@types/jest": "^29.5.13",
"dotenv": "^16.4.5",
"jest": "^29.7.0",
"prop-types": "^15.8.1",
"socket.io": "^4.8.0",
"supertest": "^7.0.0"
},
"dependencies": {
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"express": "^4.21.1",
"jsonwebtoken": "^9.0.2",
"dotenv": "^16.4.5"
"react-router-dom": "^6.27.0"
}
}
93 changes: 93 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 82d7b50

Please sign in to comment.