31 lines
948 B
Python
31 lines
948 B
Python
import asyncio
|
|
import websockets
|
|
import json
|
|
import random
|
|
import time
|
|
|
|
async def send_data(uri):
|
|
async with websockets.connect(uri) as websocket:
|
|
print("Connected to WebSocket server")
|
|
|
|
while True:
|
|
data = {
|
|
"node": "69:42:08:F5:00:00",
|
|
"Response": str(round(random.uniform(0, 2))),
|
|
"QuestionID": str(round(random.uniform(0, 90))),
|
|
}
|
|
await websocket.send(json.dumps(data))
|
|
print("Data sent")
|
|
|
|
response = await websocket.recv()
|
|
print("Received message:", response)
|
|
|
|
await asyncio.sleep(2) # Wait a bit before sending the next message
|
|
|
|
# Start the WebSocket connection
|
|
while True:
|
|
try:
|
|
asyncio.get_event_loop().run_until_complete(send_data("ws://145.92.8.114/ws"))
|
|
except Exception as e:
|
|
print("Exception:", e)
|
|
time.sleep(1) # Wait a bit before trying to reconnect |