Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
48 lines (40 loc) · 1.69 KB
/
Copy pathserver.py
File metadata and controls
48 lines (40 loc) · 1.69 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
import asyncio
import json
import math
import random
from datetime import datetime, timedelta, timezone
import websockets
# Seconds between frames. Matches `examples/server.rs` (100 ms); the previous
# 0.01 s pushed 100 points at 100 Hz, i.e. ten times the chart's redraw rate.
SEND_INTERVAL_SECS = 0.1
async def handle_client(websocket):
cnt = 0.0
# Computed once, like `examples/server.rs`: recomputing it per frame slid the
# whole x-axis window forward with the wall clock.
start_time = datetime.now(timezone.utc) - timedelta(days=7)
while True:
data = []
for i in range(100):
time = start_time + timedelta(hours=i * 2)
data.append(
{
# `isoformat()` on a tz-aware datetime already ends in
# "+00:00"; appending "Z" on top produced
# "...+00:00Z", which the Rust client's RFC 3339 parser
# rejects as trailing input, dropping every frame.
"time": time.isoformat().replace("+00:00", "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
await websocket.send(json.dumps(data))
await asyncio.sleep(SEND_INTERVAL_SECS)
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())