-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
29 lines (23 loc) · 772 Bytes
/
Copy pathserver.js
File metadata and controls
29 lines (23 loc) · 772 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
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);
// Serve static files from the public directory
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('chat message', (msg) => {
// Broadcast the message to all clients except the sender
socket.broadcast.emit('chat message', msg);
});
socket.on('disconnect', () => {
console.log('A user disconnected');
});
});
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});