简体   繁体   English

Websocket 返回状态 200 而不是 101 (flask socket.io)

[英]Websocket returns status 200 instead of 101 (flask socket.io)

I've done quite a bit of reading around to try and solve this issue but I'm still stuck.我已经做了很多阅读来尝试解决这个问题,但我仍然被卡住了。 My problem is with trying to get the websocket handshake to complete using socket.io client side and flask_socket.io server side.我的问题是尝试使用 socket.io 客户端和 flask_socket.io 服务器端完成 websocket 握手。

I can run the flask development server on my local machine using:我可以使用以下命令在本地机器上运行 Flask 开发服务器:

 app = Flask(__name__) app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' socketio = SocketIO(app) socketio.run(app) ...

and if I point chrome to localhost:5000 and press the button which I have linked to opening a websocket it works fine and chrome network tab shows status 101.如果我将 chrome 指向 localhost:5000 并按下我链接到打开 websocket 的按钮,它工作正常,chrome 网络选项卡显示状态 101。

However on uploading the code to a remote machine and again using flask's development server but changing the port to 80,然而,在将代码上传到远程机器并再次使用 Flask 的开发服务器但将端口更改为 80 时,

 socketio.run(app,host='0.0.0.0',port=80)

the websocket handshake stops working and gives status 200 instead. websocket 握手停止工作并改为提供状态 200。

WebSocket connection to 'ws://example.com/socket.io/?EIO=3&transport=websocket&sid=cfb1949b243b42578fe422782a0db359' failed: Error during WebSocket handshake: Unexpected response code: 200 WebSocket 连接到“ws://example.com/socket.io/?EIO=3&transport=websocket&sid=cfb1949b243b42578fe422782a0db359”失败:WebSocket 握手期间出错:意外响应代码:200

All websocket messages are now sent over xhr polling instead of inside the websocket frame.所有 websocket 消息现在都通过 xhr 轮询而不是在 websocket 框架内发送。

I've followed all of the guides I can find on google relating to this but with no success.我已经遵循了我可以在谷歌上找到的所有与此相关的指南,但没有成功。 I was previously using nginx and gunicorn and followed the advice to change the nginx conf to allow upgrade to websockets but that didn't solve the problem.我以前使用 nginx 和 gunicorn 并按照建议更改 nginx conf 以允许升级到 websockets 但这并没有解决问题。 So I simplified to using the flask development server but I still haven't been able to get a successful handshake.所以我简化为使用flask开发服务器,但我仍然无法成功握手。

I found a solution after finding a similar problem posted here https://nolanlawson.com/2013/05/31/web-sockets-with-socket-io-node-js-and-nginx-port-80-considered-harmful/#comment-85425 .我在发现这里发布的类似问题后找到了解决方案https://nolanlawson.com/2013/05/31/web-sockets-with-socket-io-node-js-and-nginx-port-80-thinked-harmful /#comment-85425

It turns out many public wifi networks block websockets when they are running on port 80. However often ports 443 and 8080 are not blocked.事实证明,许多公共 wifi 网络在端口 80 上运行时都会阻止 websocket。但是通常端口 443 和 8080 不会被阻止。 You can find out which ports are blocked from this website: http://websocketstest.com/ .您可以从这个网站找出哪些端口被阻止: http : //websocketstest.com/ I just moved the address of my websocket from example.com to example.com:8080.我只是将我的 websocket 的地址从 example.com 移动到 example.com:8080。

I ran into a similar problem but due to a different reason.我遇到了类似的问题,但由于不同的原因。 When using flask-socketio you have to install gevent-websocket or else it's going to use long-polling and you'll see a lot of 200 responses.使用flask-socketio您必须安装gevent-websocket ,否则它将使用long-polling ,您将看到很多200 个响应。 Thus, my solution to that problem was just.因此,我对这个问题的解决方案是公正的。

pip install gevent-websocket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM