Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Set websocket=True for Rule, fixes #81 #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tomviner
Copy link

This fixes the WebsocketMismatch which appears with Werkzeug 2.0.0.

The cause is a websocket request hits a rule which Werkzeug doesn't think is a websocket endpoint.

@tomviner
Copy link
Author

Unfortunately, Werkzeug pre 2.0.0 doesn't always label websocket requests correctly, so with this PR, you can end up with the opposite mismatch.

Here's the change to Werkzeug where more websocket requests get labelled correctly: https://github.com/pallets/werkzeug/pull/2053/files#diff-9b2e55f893697ae8ac344aebf17bebee83546fc2bf4d8e1b138cf7c63e92080fR1637-R1641

@lnstadrum
Copy link

lnstadrum commented May 19, 2021

Hi @tomviner,

I ended up with a similar change to make things work on my side, but in addition to your commit I also had to add the following change:

@@ -27,7 +27,7 @@ class SocketMiddleware(object):
     def __call__(self, environ, start_response):
         adapter = self.ws.url_map.bind_to_environ(environ)
         try:
-            handler, values = adapter.match()
+            handler, values = adapter.match(websocket=True)
             environment = environ['wsgi.websocket']
             cookie = None
             if 'HTTP_COOKIE' in environ:

In my tests this makes WebsocketMismatch disappear for a web page socket and a socket opened in Android with Kotlin Ktor API. Not sure if this is universally helpful though... My setup was as follows:

  • Python 3.8.5
  • Flask 2.0.0
  • Werkzeug 2.0.1
  • gunicorn 20.1.0

@atzannes
Copy link

@tomviner thanks for this fix. It looks like it fixes most of my problems with the Flask upgrade.

Unfortunately, Werkzeug pre 2.0.0 doesn't always label websocket requests correctly, so with this PR, you can end up with the opposite mismatch.

Here's the change to Werkzeug where more websocket requests get labelled correctly: https://github.com/pallets/werkzeug/pull/2053/files#diff-9b2e55f893697ae8ac344aebf17bebee83546fc2bf4d8e1b138cf7c63e92080fR1637-R1641

Maybe also add the constraint Flask>=2.0 in setup.py then.

@Rosuav
Copy link

Rosuav commented Jul 7, 2021

If there's an issue with misrecognizing sockets, maybe the solution would be to use the options kwargs instead?

self.url_map.add(Rule(rule, endpoint=f, **options))

And then you'd use it thus:

@sockets.route("/countdown_ctrl", websocket=True)
def control_socket(ws):

Older versions of flask_sockets would ignore that, so an app that sets this flag would be compatible with both.

@hansehe
Copy link

hansehe commented Oct 8, 2021

If there's an issue with misrecognizing sockets, maybe the solution would be to use the options kwargs instead?

self.url_map.add(Rule(rule, endpoint=f, **options))

And then you'd use it thus:

@sockets.route("/countdown_ctrl", websocket=True)
def control_socket(ws):

Older versions of flask_sockets would ignore that, so an app that sets this flag would be compatible with both.

This solution would solve it, so someone really needs to fix that! It's an easy fix, and would help a lot.
For anyone looking for a temporary solution, then you could do this:
'''
sockets.url_map.add(Rule('/mixed_ws_path', endpoint=my_websocket_function, websocket=True))
'''

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants