File: broadcast-server.py

package info (click to toggle)
opencpn 1%3A5.10.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 164,172 kB
  • sloc: ansic: 515,707; cpp: 353,066; xml: 84,559; sh: 5,343; python: 1,833; makefile: 101; perl: 83; cs: 65; javascript: 65; ruby: 21
file content (28 lines) | stat: -rw-r--r-- 555 bytes parent folder | download | duplicates (4)
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
#!/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()