Handle a WebSocket client connection
(self, websocket, path=None)
| 145 | pass |
| 146 | |
| 147 | async def handle_client(self, websocket, path=None): |
| 148 | """Handle a WebSocket client connection""" |
| 149 | try: |
| 150 | self.connected_clients.add(websocket) |
| 151 | client_id = id(websocket) |
| 152 | |
| 153 | # Initialize TermNet for this client |
| 154 | await self.initialize_termnet() |
| 155 | |
| 156 | # Send welcome message |
| 157 | welcome_msg = { |
| 158 | "type": "system", |
| 159 | "message": "TermNet v1.2 ready - WebSocket connection established", |
| 160 | "timestamp": time.time() |
| 161 | } |
| 162 | try: |
| 163 | await websocket.send(json.dumps(welcome_msg)) |
| 164 | except: |
| 165 | return |
| 166 | |
| 167 | async for message in websocket: |
| 168 | try: |
| 169 | data = json.loads(message) |
| 170 | user_input = data.get("message", "").strip() |
| 171 | |
| 172 | if not user_input: |
| 173 | continue |
| 174 | |
| 175 | if user_input.lower() in ("exit", "quit", "close"): |
| 176 | break |
| 177 | |
| 178 | # Stream the response back to client |
| 179 | await self.stream_agent_response(websocket, user_input) |
| 180 | |
| 181 | except json.JSONDecodeError: |
| 182 | try: |
| 183 | error_msg = { |
| 184 | "type": "error", |
| 185 | "message": "Invalid JSON format", |
| 186 | "timestamp": time.time() |
| 187 | } |
| 188 | await websocket.send(json.dumps(error_msg)) |
| 189 | except: |
| 190 | pass |
| 191 | except Exception: |
| 192 | # Silently ignore all other exceptions |
| 193 | pass |
| 194 | |
| 195 | except Exception: |
| 196 | # Silently handle all connection exceptions |
| 197 | pass |
| 198 | finally: |
| 199 | try: |
| 200 | self.connected_clients.discard(websocket) |
| 201 | except: |
| 202 | pass |
| 203 | |
| 204 | async def start_server(self): |
nothing calls this directly
no test coverage detected