-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_websocket.py
More file actions
49 lines (39 loc) · 1.66 KB
/
test_websocket.py
File metadata and controls
49 lines (39 loc) · 1.66 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
49
#!/usr/bin/env python3
"""
WebSocket Test Scripti
"""
import websockets
import asyncio
import json
async def test_websocket():
"""WebSocket bağlantısını test et"""
print("🔍 WebSocket bağlantısı test ediliyor...")
try:
uri = "ws://localhost:8000/ws/temperature"
async with websockets.connect(uri) as websocket:
print("✅ WebSocket bağlantısı başarılı!")
print("📡 Mesajlar dinleniyor...")
# 10 saniye boyunca mesajları dinle
start_time = asyncio.get_event_loop().time()
message_count = 0
while asyncio.get_event_loop().time() - start_time < 10:
try:
message = await asyncio.wait_for(websocket.recv(), timeout=1)
data = json.loads(message)
if data['type'] == 'temperature_update':
temp_value = data['data']['temperature']['value']
timestamp = data['timestamp']
print(f"📊 Sıcaklık: {temp_value}°C | {timestamp}")
message_count += 1
except asyncio.TimeoutError:
continue
except Exception as e:
print(f"❌ Mesaj alma hatası: {e}")
break
print(f"✅ {message_count} WebSocket mesajı alındı!")
return message_count > 0
except Exception as e:
print(f"❌ WebSocket bağlantı hatası: {e}")
return False
if __name__ == "__main__":
asyncio.run(test_websocket())