mirror of
https://github.com/KevinMidboe/delugeClient.git
synced 2025-10-29 12:00:13 +00:00
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:
@@ -7,21 +7,21 @@ async def hello(websocket, path):
|
|||||||
name = await websocket.recv()
|
name = await websocket.recv()
|
||||||
print(f"< {name}")
|
print(f"< {name}")
|
||||||
|
|
||||||
greeting = f"Hello {name}!"
|
greeting = f"Hello {name}, welcome to the world websockets!"
|
||||||
|
|
||||||
await websocket.send(greeting)
|
await websocket.send(greeting)
|
||||||
print(f"> {greeting}")
|
print(f"> {greeting}")
|
||||||
|
|
||||||
# start_server = websocket.serve(hello, 'localhost', 8765)
|
|
||||||
|
|
||||||
async def time(websocket, path):
|
async def time(websocket, path):
|
||||||
while True:
|
while True:
|
||||||
now = datetime.datetime.utcnow().isoformat() + 'Z'
|
now = datetime.datetime.utcnow().isoformat() + 'Z'
|
||||||
await websocket.send(now)
|
await websocket.send(now)
|
||||||
# await asyncio.sleep(random.random() * 3)
|
|
||||||
await asyncio.sleep(1)
|
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()
|
asyncio.get_event_loop().run_forever()
|
||||||
|
|||||||
Reference in New Issue
Block a user