Skip to content

Commit 7079279

Browse files
committedApr 14, 2024
Add express, cors and page index for up status
1 parent a4c8215 commit 7079279

File tree

4 files changed

+472
-11
lines changed

4 files changed

+472
-11
lines changed
 

‎index.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Multi-game-server</title>
5+
<style>
6+
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
7+
</style>
8+
</head>
9+
<body>
10+
<h1>Server is up!</h1>
11+
</body>
12+
</html>

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"license": "MIT",
77
"private": true,
88
"scripts": {
9-
"start": "node server.js",
9+
"start": "node server.js",
1010
"test": "echo \"Error: no test specified\" && exit 1"
1111
},
1212
"dependencies": {
13+
"express": "^4.19.2",
1314
"socket.io": "^4.7.5"
1415
}
1516
}

‎server.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
1-
const { Server } = require("socket.io");
1+
const { Server } = require('socket.io');
2+
const express = require('express');
3+
const { createServer } = require('http');
4+
const cors = require('cors');
25

3-
const io = new Server({ /* options */ });
6+
const app = express();
7+
app.use(cors());
48

5-
io.on("connection", (socket) => {
9+
app.get('/', (req, res) => {
10+
res.sendFile(__dirname + '/index.html');
11+
// res.send('<h1>Worker up</h1>');
12+
});
13+
14+
const httpServer = createServer(app);
15+
const io = new Server(httpServer, {
16+
cors: {
17+
origin: ['https://cnotv-multi-game.netlify.app/', 'http://localhost:4000/'],
18+
methods: ['GET', 'POST'],
19+
},
20+
});
21+
22+
io.on('connection', (socket) => {
623
const count = io.engine.clientsCount;
7-
const uuid = require("uuid");
24+
const uuid = require('uuid');
825

926
io.engine.generateId = (req) => {
1027
return uuid.v4(); // must be unique across all Socket.IO servers
1128
}
1229

1330
console.log(`New connection: ${socket.id} - Total clients: ${count}`);
1431

15-
io.engine.on("connection_error", (err) => {
32+
io.engine.on('connection_error', (err) => {
1633
console.log(err);
1734
});
1835
});
1936

20-
21-
io.listen(3000);
37+
httpServer.listen(3000, () => {
38+
console.log('Server ready');
39+
});

0 commit comments

Comments
 (0)