Added example usage for deluge_socket connection. Includes to endpoints that send a timestamp from server to client every second and to send a response to receiving a message from client.

This commit is contained in:
2018-08-12 17:18:58 +02:00
parent a5eb22ae5a
commit 5ed1c6ebdf

View File

@@ -7,21 +7,21 @@ async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
greeting = f"Hello {name}, welcome to the world websockets!"
await websocket.send(greeting)
print(f"> {greeting}")
# start_server = websocket.serve(hello, 'localhost', 8765)
async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + 'Z'
await websocket.send(now)
# await asyncio.sleep(random.random() * 3)
await asyncio.sleep(1)
start_server = websockets.serve(time, 'localhost', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
serve_hello = websockets.serve(hello, 'localhost', 8765)
serve_time = websockets.serve(time, 'localhost', 5678)
asyncio.get_event_loop().run_until_complete(serve_hello)
asyncio.get_event_loop().run_until_complete(serve_time)
asyncio.get_event_loop().run_forever()