OpenC3 / cosmos

OpenC3 COSMOS
https://openc3.com
Other
103 stars 30 forks source link

Python Websocket Example not working #1535

Open smcHannah opened 5 days ago

smcHannah commented 5 days ago

Describe the bug The Python WebSocket API attempts to connect with a truncated authentication token, which results in a ConnectionClosedOK exception saying "received 1000 (OK); then sent 1000 (OK)" when running cosmos_web_socket_example.py with COSMOS Open Source.

To Reproduce

  1. Change the OpenC3 API password to my own in cosmos_web_socket_example.py
  2. Run cosmos_web_socket_example.py
  3. Get the following traceback: Traceback (most recent call last): File "c:\Users\...\cosmos_web_socket_example.py", line 24, in <module> with StreamingWebSocketApi() as api: File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\openc3\script\web_socket_api.py", line 62, in __exit__ self.disconnect() File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\openc3\script\web_socket_api.py", line 153, in disconnect self.unsubscribe() File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\openc3\script\web_socket_api.py", line 110, in unsubscribe self.stream.write(json.dumps(json_hash)) File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\openc3\streams\web_socket_client_stream.py", line 50, in write self.connection.send(data) File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\sync\connection.py", line 281, in send with self.send_context(): File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 137, in __enter__ return next(self.gen) ^^^^^^^^^^^^^^ File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\sync\connection.py", line 715, in send_context raise self.protocol.close_exc from original_exc websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)
  4. Change line 131 of web_socket_api.py from final_url = self.url + f"?scope={self.scope}&authorization={self.authentication.token()[7:]}" to final_url = self.url + f"?scope={self.scope}&authorization={self.authentication.token()}"
  5. Run cosmos_web_socket_example.py again
  6. Successfully receive and print all the requested items.

Environment

jmthomas commented 5 days ago

Hmm, this change was introduced in #1424.