|
const express = require('express'); |
|
const http = require('http'); |
|
const socketIo = require('socket.io'); |
|
|
|
const app = express(); |
|
const server = http.createServer(app); |
|
const io = socketIo(server); |
|
|
|
let visitorCount = 0; |
|
|
|
app.get('/', (req, res) => { |
|
res.json({ message: 'WebSocket server is running', visitorCount }); |
|
}); |
|
|
|
io.on('connection', (socket) => { |
|
visitorCount++; |
|
io.emit('visitorCount', visitorCount); |
|
|
|
socket.on('disconnect', () => { |
|
visitorCount--; |
|
io.emit('visitorCount', visitorCount); |
|
}); |
|
}); |
|
|
|
const PORT = process.env.PORT || 7860; |
|
server.listen(PORT, () => { |
|
console.log(`Server running on http://localhost:${PORT}`); |
|
}); |