-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_ping.py
35 lines (30 loc) · 1.06 KB
/
ws_ping.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
import asyncio
import websockets
import time
import logging
from os import environ
# WebSocket server URL
ads_server_url = environ.get("ADS_SERVER_URL")
ads_server_domain = ads_server_url.split("://")[1]
SERVER_URL = f"wss://{ads_server_domain}:9090"
async def connect_to_server():
try:
async with websockets.connect(SERVER_URL) as websocket:
await websocket.send("ping")
msg = await websocket.recv()
assert msg.strip() == "pong"
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f"Connected successfully to server at {current_time}")
except AssertionError:
print(f"Error: Server did not respond with 'pong', instead got: {msg}")
except Exception as e:
print(f"Error connecting to server: {e}")
def main():
while True:
try:
asyncio.run(connect_to_server())
except Exception as e:
print(f"Error running asyncio: {e}")
time.sleep(60) # Wait for 1 minute (60 seconds)
if __name__ == "__main__":
main()