-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (27 loc) · 928 Bytes
/
server.js
File metadata and controls
33 lines (27 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const path = require('path');
app.use(express.static('public'));
// Serve same HTML for any room URL
app.get('/:room', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// In app.js or server.js
io.on('connection', socket => {
socket.on('join room', room => {
socket.join(room);
console.log(`User joined room: ${room}`);
socket.on('chat message', (msg) => {
const room = msg.room;
io.to(room).emit('chat message', msg); // Send full object back
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
});
http.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});