Replies: 15 comments
-
Can you please create a short and simple example application that demonstrates the issue? The latest Flask and Werkzeug versions should be supported. |
Beta Was this translation helpful? Give feedback.
-
yes, i didn't manage to reproduce it outside of the legacy application, i am still trying to reproduce it via a simple app which will also help me isolate the issue. |
Beta Was this translation helpful? Give feedback.
-
The difference in the flows is after the accept connection (which is the same for both success and failure applications), the server sends 'b'\x88\x1a\x03\xeaWebSocket Protocol Error'' to the client |
Beta Was this translation helpful? Give feedback.
-
I think it is going to be very difficult to debug this just by looking at the bytes. What the server sends is a standard protocol error. What matters is what made the server send this error, which must have happened before. |
Beta Was this translation helpful? Give feedback.
-
is there a focal point to find what happens after the 'accept connection' phase? |
Beta Was this translation helpful? Give feedback.
-
The WebSocket logic is not in this repository, you will need to look at the wsproto package, where the low-level protocol handshakes are implemented. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@barv-di There is a gevent example in this repo. You are not doing this correctly. Two problems, a) you need to monkey patch the standard library for Gevent compatibility and b) you should not use gevent-websocket with this package. Your fixed example would be: from gevent import monkey
monkey.patch_all()
from flask import Flask, render_template
from flask_sock import Sock
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 25}
sock = Sock(app)
@app.route('/')
def index():
return render_template('index.html')
def _register_websocket_endpoints():
@sock.route('/echo')
def echo(ws):
_handle_websocket_connection(ws)
def _handle_websocket_connection(websocket_connection):
while True:
websocket_connection.receive()
websocket_connection.send("bar")
if __name__ == "__main__":
_register_websocket_endpoints()
WSGIServer(('0.0.0.0', 11206), app).serve_forever() |
Beta Was this translation helpful? Give feedback.
-
Running this still not works (closing state) the same error. |
Beta Was this translation helpful? Give feedback.
-
See the documentation for how to run with gunicorn: https://flask-sock.readthedocs.io/en/latest/web_servers.html#id3 |
Beta Was this translation helpful? Give feedback.
-
Running the code in previous post along with I attach the pip3 list output (maybe useful?)
|
Beta Was this translation helpful? Give feedback.
-
The code that I shown above is written to be used with the gevent web server, not gunicorn. I based it on your own code. Does it work that way? Because you are registering your endpoints inside the if condition at the bottom. That code does not run when Gunicorn imports your app. |
Beta Was this translation helpful? Give feedback.
-
@miguelgrinberg I'm joining this conversation, there is random behaviour with error:
Error is shown after first send (from client) and receiving message (from server). Browser javascript code:
flask code from flask_cors import CORS app = Flask("Exear", static_url_path='') websocket = Sock(app) @websocket.route("/ws")
app.run(debug=True, host="0.0.0.0") |
Beta Was this translation helpful? Give feedback.
-
@erikpa1 Try with a production web server. You are using the Flask development web server for this and on top of that you are using development mode in your app. Also your websocket handler exits after receiving and sending. Is this intended? The connection will close if you let the function end. |
Beta Was this translation helpful? Give feedback.
-
@miguelgrinberg yeah sorry, I came to write, it was because I was missing |
Beta Was this translation helpful? Give feedback.
-
I am trying to upgrade ,an application i am working on from flask-socket to flask-sock a while ago flask and werkzeug were upgraded from very old versions to 2.2.2 for both and caused the websocket to didn't work.
Following the gevent example works on an isolated environment, while at my app, during the connection handshake, the underlaying simple-websocket sends an AcceptConnection which resulted with error (1002 protocol error).
The original request:
The response:
At this point the client (tried multiple ones) sees the following error:
Invalid frame header
While the flask server returns
I tried to debug both raw example and my app, both send the exact same AcceptConnection response (only difference was the token's value). however my application closes the connection during the handshake.
I couldn't find evidence for a middleware which messes with the headers/buffer.
I did find this comment.
My question is if there is a support for flask and werkzeug 2.2.2 with this library and if there is a reason for the flask route to reject a message sent during the handshake
Beta Was this translation helpful? Give feedback.
All reactions