-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
34 lines (26 loc) · 931 Bytes
/
server.js
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
34
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
app.use(express.static('public'));
wss.on('connection', (ws) => {
console.log('New user logged in');
ws.on('message', (message) => {
console.log(`Received: ${message}`);
const messageString = typeof message === 'string' ? message : message.toString();
// Send the new message only to connected clients
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(messageString); // Send only the new message
}
});
});
ws.on('close', () => {
console.log('User logged out');
});
});
server.listen(3500, () => {
console.log('Server running on ws://localhost:3500');
});