-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
39 lines (32 loc) · 1.12 KB
/
server.py
File metadata and controls
39 lines (32 loc) · 1.12 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
import asyncio
import json
import math
import random
from datetime import datetime, timedelta, timezone
import websockets
async def handle_client(websocket):
cnt = 0.0
while True:
start_time = datetime.now(timezone.utc) - timedelta(days=7)
data = []
for i in range(100):
time = start_time + timedelta(hours=i * 2)
data.append(
{
"time": time.isoformat() + "Z",
"y1": math.sin(i / 10 + cnt)
+ random.uniform(-0.2, 0.2), # Sine wave with random noise
"y2": math.sin(i / 5 + cnt) * 0.8
+ random.uniform(-0.1, 0.1), # Different frequency sine wave
}
)
cnt += 1.0
print("data.len=", len(data))
await websocket.send(json.dumps(data))
await asyncio.sleep(0.01)
async def main():
async with websockets.serve(handle_client, "127.0.0.1", 8080):
print("WebSocket server started on ws://127.0.0.1:8080")
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())