MCPcopy Create free account
hub / github.com/dbunt1tled/python-fast-api / wss

Method wss

src/app/ws/route/ws_controller.py:15–39  ·  view source on GitHub ↗
(
        self,
        websocket: WebSocket,
        user_id: str,
    )

Source from the content-addressed store, hash-verified

13 app.add_api_websocket_route(path="/ws/{user_id}", endpoint=self.wss)
14
15 async def wss(
16 self,
17 websocket: WebSocket,
18 user_id: str,
19 ) -> None:
20 ws_service = self.container.ws_service()
21 try:
22 await ws_service.add_connection(user_id, websocket)
23 async for data in websocket.iter_text():
24 try:
25 message = json.loads(data)
26 await ws_service.process_message(user_id=user_id, message=message, websocket=websocket)
27 except WebSocketDisconnect:
28 break
29 except json.JSONDecodeError:
30 self.logger.warning(f"Invalid JSON received from user {user_id}")
31 await ws_service.ws_manager.send_to_user(user_id=user_id, data={"message": "Incoming message is not valid"}, websocket=websocket)
32 except Exception as e:
33 self.logger.error(f"Error handling WebSocket message from user {user_id}: {e}")
34 break
35
36 except Exception as e:
37 self.logger.error(f"WebSocket error for user {user_id}: {e}")
38 finally:
39 await ws_service.remove_connection(user_id=user_id, websocket=websocket)

Callers

nothing calls this directly

Calls 6

warningMethod · 0.80
send_to_userMethod · 0.80
add_connectionMethod · 0.45
process_messageMethod · 0.45
errorMethod · 0.45
remove_connectionMethod · 0.45

Tested by

no test coverage detected