# testscript for websocket server 
# stolen from Sam's last project
import websockets
import asyncio

connected = set()

async def handler(websocket):
    # Register.
    connected.add(websocket)
    try:
        while True:
            message = await websocket.recv()
            print(message)
    except websockets.ConnectionClosedOK:
        print("Client disconnected")
    finally:
        connected.remove(websocket)


async def main():
    async with websockets.serve(handler, "0.0.0.0", 8001):
        await asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(main())    

#https://websockets.readthedocs.io/en/stable/reference/sync/server.html#websockets.sync.server.serve