-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (45 loc) · 1.67 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import List
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
app = FastAPI()
class Coordinate(BaseModel):
latitude: float = 0
longitude: float = 0
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
self.client_ids: list[int] = []
self.markers: dict = {}
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def broadcast(self, client_id: int, coordinate: str):
self.client_ids.append(client_id)
self.markers[client_id] = {
"coordinate": coordinate,
"marker_size": self.client_ids.index(client_id) + 40,
}
for connection in self.active_connections:
await connection.send_json({
"message": f"user {client_id}'s location: {coordinate}",
"markers": self.markers
})
manager = ConnectionManager()
@app.get("/")
async def get():
with open("viewer.html", "r", encoding='utf-8') as f:
html = f.read()
return HTMLResponse(html)
@app.websocket("/ws/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: int):
await manager.connect(websocket)
try:
while True:
text = await websocket.receive_text()
await manager.broadcast(client_id, text)
except WebSocketDisconnect:
manager.disconnect(websocket)
del manager.markers[client_id]