Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebsocketMismatch #4299

Closed
hansehe opened this issue Oct 8, 2021 · 2 comments
Closed

WebsocketMismatch #4299

hansehe opened this issue Oct 8, 2021 · 2 comments

Comments

@hansehe
Copy link

hansehe commented Oct 8, 2021

New version of Flask (>1.1.4) does not work with Flask-Sockets (websockets).

Replicate bug by running server and client.

requirements:

Flask==2.0.2
Flask-Sockets==0.2.1
Werkzeug==2.0.2
gevent==21.8.0
websocket-client==1.2.1

flash_ws_server.py:

from flask import Flask
from flask_sockets import Sockets


app = Flask(__name__)
sockets = Sockets(app)


@sockets.route('/mixed_ws_path')
def echo_socket(ws):
    while not ws.closed:
        message = ws.receive()
        ws.send(message)


@app.route('/mixed_ws_path')
def hello():
    return 'Hello World!'


if __name__ == "__main__":
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
    server.serve_forever()

flask_ws_client.py:

import websocket
import _thread
import time


def on_message(ws, message):
    print(message)


def on_error(ws, error):
    print(error)


def on_close(ws, close_status_code, close_msg):
    print("### closed ###")


def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    _thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://localhost:5000/mixed_ws_path",
                                on_open=on_open,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)

    ws.run_forever()

The server responds with this exception:

Traceback (most recent call last):
  File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\pywsgi.py", line 999, in handle_one_response
    self.run_application()
  File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\geventwebsocket\handler.py", line 75, in run_application
    self.run_websocket()
  File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\geventwebsocket\handler.py", line 52, in run_websocket
    list(self.application(self.environ, lambda s, h, e=None: []))
  File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\flask_sockets.py", line 40, in __call__
    handler, values = adapter.match()
  File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\werkzeug\routing.py", line 2039, in match
    raise WebsocketMismatch()
werkzeug.routing.WebsocketMismatch: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
2021-10-08T09:59:35Z {'REMOTE_ADDR': '::1', 'REMOTE_PORT': '56214', 'HTTP_HOST': 'localhost:5000', (hidden keys: 24)} failed with WebsocketMismatch

Everything works fine with Flask version ==1.1.4

Environment:

  • Python version: 3.9.0
  • Flask version: 2.0.2
@ThiefMaster
Copy link
Member

ThiefMaster commented Oct 8, 2021

Sounds more like something to report on the flask-sockets repo to me...

Edit: https://github.com/heroku-python/flask-sockets looks pretty dead to me so you may want to look for an alternative..
And someone already reported this in heroku-python/flask-sockets#81

@hansehe
Copy link
Author

hansehe commented Oct 8, 2021

Ok thanks! Too bad nobody looks after it.
I found a temporary solution though:
heroku-python/flask-sockets#82 (comment)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants