miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
272 stars 24 forks source link

Route with path parameters not work? #81

Closed mirusu400 closed 2 months ago

mirusu400 commented 2 months ago

I want to make simple websocket server, with path parameters like:

@sock.route("/test/<path>")
def handle_socket(ws):
    print("WebSocket connected")
    while True:
        data = ws.receive()
        if data == 'close':
            break
        ws.send(data)

if __name__ == "__main__":
    server = pywsgi.WSGIServer(("localhost", 7301), app)
    server.serve_forever()

But it seems they got error like:

[2024-05-10 16:06:26,958] ERROR in app: Exception on /test/r4zq6nq6gaqtzpd3 [GET]
Traceback (most recent call last):
  File "C:\Users\OWNER\Documents\_github\venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\OWNER\Documents\_githubvenv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\OWNER\Documents\_github\venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\OWNER\Documents\_github\venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\OWNER\Documents\_github\venv\Lib\site-packages\flask_sock\__init__.py", line 62, in websocket_route
    f(ws, *args, **kwargs)
TypeError: handle_socket() got an unexpected keyword argument 'path'

So, the question is: flask-sock doesn't allow path paramers? Or any solutions for resolve this issue?

miguelgrinberg commented 2 months ago

Try this:

@sock.route("/test/<path>")
def handle_socket(ws, path):
    # ...
mirusu400 commented 2 months ago

Oops my miss 😅 Thank you!