cleanup
This commit is contained in:
28
ws/broadcast-server.js
Normal file
28
ws/broadcast-server.js
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* broadcast-server.js
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080, perMessageDeflate: true });
|
||||
|
||||
// Broadcast to all.
|
||||
wss.broadcast = function broadcast(data) {
|
||||
wss.clients.forEach(function each(client) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', function incoming(data) {
|
||||
// Broadcast to everyone else.
|
||||
wss.clients.forEach(function each(client) {
|
||||
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
||||
client.send(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
28
ws/broadcast-server.py
Normal file
28
ws/broadcast-server.py
Normal file
@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
connections = set()
|
||||
|
||||
async def echo(websocket, path):
|
||||
|
||||
connections.add(websocket)
|
||||
|
||||
try:
|
||||
async for message in websocket:
|
||||
print(message)
|
||||
|
||||
for ws in connections:
|
||||
if ws != websocket:
|
||||
await ws.send(message)
|
||||
except:
|
||||
raise
|
||||
finally:
|
||||
connections.remove(websocket)
|
||||
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(
|
||||
websockets.serve(echo, 'localhost', 8080))
|
||||
asyncio.get_event_loop().run_forever()
|
Reference in New Issue
Block a user