-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws_producer_client.py
44 lines (27 loc) · 991 Bytes
/
ws_producer_client.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
websocket client which keeps sending payload to the server.
"""
import asyncio
import datetime
import json
import random
import websockets
URI = "ws://localhost:8000/ws/producer"
async def initiate_production():
try:
async with websockets.connect(URI) as websocket:
while True:
payload = {
"timestamp": str(datetime.datetime.now()),
"value": random.randint(0, 100),
}
payload = json.dumps(payload)
await websocket.send(payload)
print(f"-> payload_sent :: {payload}")
recv_payload = await websocket.recv()
recv_payload = json.loads(recv_payload)
await asyncio.sleep(1)
except websockets.exceptions.ConnectionClosedError as e:
print(e, "websocket unexpectedly closed!")
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(initiate_production())