Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const http = require('http');
|
3 |
+
const socketIo = require('socket.io');
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
const server = http.createServer(app);
|
7 |
+
const io = socketIo(server);
|
8 |
+
|
9 |
+
let visitorCount = 0;
|
10 |
+
|
11 |
+
// Simple endpoint to verify server is running
|
12 |
+
app.get('/', (req, res) => {
|
13 |
+
res.json({ message: 'WebSocket server is running', visitorCount });
|
14 |
+
});
|
15 |
+
|
16 |
+
io.on('connection', (socket) => {
|
17 |
+
visitorCount++;
|
18 |
+
io.emit('visitorCount', visitorCount); // Broadcast updated count to all clients
|
19 |
+
|
20 |
+
socket.on('disconnect', () => {
|
21 |
+
visitorCount--;
|
22 |
+
io.emit('visitorCount', visitorCount); // Broadcast updated count
|
23 |
+
});
|
24 |
+
});
|
25 |
+
|
26 |
+
server.listen(3000, () => {
|
27 |
+
console.log('Server running on http://localhost:3000');
|
28 |
+
});
|