-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_websocket.py
More file actions
48 lines (38 loc) · 1.38 KB
/
test_websocket.py
File metadata and controls
48 lines (38 loc) · 1.38 KB
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
45
46
47
48
#!/usr/bin/env python3
"""
Test WebSocket connection to Valtronics backend
"""
import asyncio
import websockets
import json
import sys
async def test_websocket():
"""Test WebSocket connection and message handling"""
uri = "ws://localhost:8000/ws"
try:
print(f"🔌 Connecting to WebSocket: {uri}")
async with websockets.connect(uri) as websocket:
print("✅ WebSocket connection established!")
# Send a test message
test_message = {
"type": "ping",
"timestamp": "2026-05-04T16:10:00Z"
}
await websocket.send(json.dumps(test_message))
print(f"📤 Sent: {test_message}")
# Wait for response
response = await asyncio.wait_for(websocket.recv(), timeout=5.0)
print(f"📥 Received: {response}")
return True
except websockets.exceptions.ConnectionRefused:
print("❌ WebSocket connection refused - server may not be running")
return False
except asyncio.TimeoutError:
print("⏰ WebSocket response timeout")
return False
except Exception as e:
print(f"❌ WebSocket error: {e}")
return False
if __name__ == "__main__":
success = asyncio.run(test_websocket())
sys.exit(0 if success else 1)