Debian Codegraph @

LspCpp/third_party/ixwebsocket/ws/broadcast-server.js

raw
1 /*
2 * broadcast-server.js
3 * Author: Benjamin Sergeant
4 * Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
5 */
6 const WebSocket = require('ws');
7
8 const wss = new WebSocket.Server({ port: 8080, perMessageDeflate: true });
9
10 // Broadcast to all.
11 wss.broadcast = function broadcast(data) {
12 wss.clients.forEach(function each(client) {
13 if (client.readyState === WebSocket.OPEN) {
14 client.send(data);
15 }
16 });
17 };
18
19 wss.on('connection', function connection(ws) {
20 ws.on('message', function incoming(data) {
21 // Broadcast to everyone else.
22 wss.clients.forEach(function each(client) {
23 if (client !== ws && client.readyState === WebSocket.OPEN) {
24 client.send(data);
25 }
26 });
27 });
28 });