You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
783 B
30 lines
783 B
const WebSocket = require('ws');
|
|
const uuid = require('node-uuid');
|
|
|
|
const wss = new WebSocket.Server({ port: 8080 });
|
|
|
|
const DEBUG = true;
|
|
|
|
const Server = {
|
|
messageAll: (ws, message) => {
|
|
wss.clients.forEach((client) => {
|
|
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
|
client.send(ws.id + ' ' + message);
|
|
}
|
|
});
|
|
},
|
|
|
|
onMessage: (ws, message) => {
|
|
DEBUG && console.log('Received: %s', message);
|
|
Server.messageAll(ws, message);
|
|
},
|
|
};
|
|
|
|
wss.on('connection', function connection(ws, req) {
|
|
ws.id = uuid.v4();
|
|
DEBUG && console.log(req.url + ' connected to ' + ws.id);
|
|
|
|
ws.on('message', Server.onMessage.bind(null, ws));
|
|
});
|
|
|
|
console.log("Websocket server listening on :8080") |