-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebsocket.js
More file actions
41 lines (33 loc) · 1.29 KB
/
Websocket.js
File metadata and controls
41 lines (33 loc) · 1.29 KB
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
35
36
37
38
39
40
41
const WebSocket = require('ws');
// This file sets up a basic WebSocket server using the "ws" library.
// Make sure to install the "ws" package via npm: npm install ws
const PORT = 8080;
const server = new WebSocket.Server({ port: PORT });
const clients = new Set();
server.on('connection', (ws) => {
console.log('A new client connected.');
clients.add(ws);
ws.on('message', (data) => {
console.log(`Received: ${data}`);
// Broadcast received messages to all connected clients
clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
const req = arguments[1];
if (!req || !req.headers.origin || (req.headers.origin !== 'https://matrixrainsimulatorofficial.github.io' && !req.headers.origin.match(/^https?:\/\//) && !req.url.endsWith('.html'))) {
console.log('Connection rejected: Only HTML clients are allowed.');
ws.close();
return;
}
ws.on('close', () => {
console.log('Client disconnected.');
clients.delete(ws);
});
ws.on('error', (err) => {
console.error('WebSocket error:', err);
});
});
console.log(`WebSocket server is listening on ws://localhost:${PORT}`);