Callback function called when a message is received from the server. It updates various attributes of the client based on the received message, including recording status, language detection, and server messages. If a disconnect message is received, it sets the reco
(self, ws, message)
| 234 | |
| 235 | |
| 236 | def on_message(self, ws, message): |
| 237 | """ |
| 238 | Callback function called when a message is received from the server. |
| 239 | |
| 240 | It updates various attributes of the client based on the received message, including |
| 241 | recording status, language detection, and server messages. If a disconnect message |
| 242 | is received, it sets the recording status to False. |
| 243 | |
| 244 | Args: |
| 245 | ws (websocket.WebSocketApp): The WebSocket client instance. |
| 246 | message (str): The received message from the server. |
| 247 | |
| 248 | """ |
| 249 | message = json.loads(message) |
| 250 | |
| 251 | if self.uid != message.get("uid"): |
| 252 | print("[ERROR]: invalid client uid") |
| 253 | return |
| 254 | |
| 255 | if "status" in message.keys(): |
| 256 | self.handle_status_messages(message) |
| 257 | return |
| 258 | |
| 259 | if "message" in message.keys() and message["message"] == "DISCONNECT": |
| 260 | print("[INFO]: Server disconnected due to overtime.") |
| 261 | self.recording = False |
| 262 | |
| 263 | if "message" in message.keys() and message["message"] == "SERVER_READY": |
| 264 | self.last_response_received = time.time() |
| 265 | self.recording = True |
| 266 | self.server_backend = message["backend"] |
| 267 | print(f"[INFO]: Server Running with backend {self.server_backend}") |
| 268 | return |
| 269 | |
| 270 | if "language" in message.keys(): |
| 271 | self.language = message.get("language") |
| 272 | lang_prob = message.get("language_prob") |
| 273 | print( |
| 274 | f"[INFO]: Server detected language {self.language} with probability {lang_prob}" |
| 275 | ) |
| 276 | return |
| 277 | |
| 278 | if "segments" in message.keys(): |
| 279 | self.process_segments(message["segments"]) |
| 280 | |
| 281 | if "translated_segments" in message.keys(): |
| 282 | self.process_segments(message["translated_segments"], translated=True) |
| 283 | |
| 284 | def on_error(self, ws, error): |
| 285 | print(f"[ERROR] WebSocket Error: {error}") |