最新消息:20210917 已从crifan.com换到crifan.org

【未解决】Simple WebSocket Client测试Flask的websocket显示OPENING且弹出undefined对话框

Flask crifan 6301浏览 0评论

折腾:

【已解决】flask中实现websocket

期间,用Chrome的插件

Simple WebSocket Client

去测试Flask的websocket:

ws://115.29.173.126:21085/test

结果显示OPENING,并弹出对话框显示:

Simple WebSocket Client

undefined

不清楚是到底什么原因。

搜:

Simple WebSocket Client OPENING undefined

参考:

websocket always triggers onerror(undefined) with Chrome, but not with Firefox – Stack Overflow

WebSockets – A Quick Introduction and a Sample Application

算了,再去找找其它的websocket的测试工具

搜:

websocket tool

参考:

Inspecting WebSocket Traffic with Chrome Developer Tools

Debugging WebSocket in Google Chrome – Stack Overflow

然后看到之前的错误是:

index.js:15 WebSocket connection to ‘ws://115.29.173.126:21085/test’ failed: Error during WebSocket handshake: Unexpected response code: 404

websocket.org Echo Test – Powered by Kaazing

结果也是连接失败:

ws://115.29.173.126:21085/test
ERROR: undefined
DISCONNECTED

Websockets | Down and Dirty with Chrome Developer Tools

后续要测试ws时去试试:

GitHub – observing/thor: The WebSocket god of thunder

另外有空再去试试Firefox的ws插件:

https://addons.mozilla.org/zh-cn/firefox/addon/websocket-monitor/

搜:

Flask-SocketIO WebSocket connection to failed Error during WebSocket handshake Unexpected response code 404

参考:

Error during WebSocket handshake: Unexpected response code: 400 · Issue #1942 · socketio/socket.io · GitHub

需要去改Nginx的配置?

heroku – Flask-SocketIO Error during websocket handshake Unexpected response code: 400 – Stack Overflow

gunicorn 19 with WebSocket on Heroku · Issue #1147 · benoitc/gunicorn · GitHub

->

我这里也是gunicorn 19

(RunningFast) ➜  staging gunicorn –version
gunicorn (version 19.6.0)
(RunningFast) ➜  staging which gunicorn
/root/Envs/RunningFast/bin/gunicorn

-》抽空再去试试换成18,是否能解决问题

结果也证明不行:

【未解决】把gunicorn从19.6降级为18.0后supervisorctl运行出错:gunicorn: error: No application module specified

pyramid – gevent-socketio – Error during WebSocket handshake: Unexpected response code: 401 – Stack Overflow

socket.io – Error during WebSocket handshake: Unexpected response code: 400 on free OpenShift MEAN stack server – Stack Overflow

sometimes, it takes really very long time to connect on heroku · Issue #171 · miguelgrinberg/Flask-SocketIO · GitHub

去试试:

降级gunicorn为18试试:

【已解决】Flask的虚拟环境中把gunicorn的版本从19降低为18

结果启动Flask的app都出错了:

【未解决】把gunicorn从19.6降级为18.0后supervisorctl运行出错:gunicorn: error: No application module specified

算了,不折腾了。最后还是换回最新的gunicorn:

(RunningFast) ➜  staging pip install gunicorn      
Collecting gunicorn
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by ‘ReadTimeoutError("HTTPSConnectionPool(host=’pypi.python.org’, port=443): Read timed out. (read timeout=15)",)’: /simple/gunicorn/
  Using cached gunicorn-19.6.0-py2.py3-none-any.whl
Installing collected packages: gunicorn
Successfully installed gunicorn-19.6.0

至少保证gunicorn正常工作:

(RunningFast) ➜  staging supervisorctl -c supervisor.conf restart runningfast_staging
runningfast_staging: stopped
runningfast_staging: started

Flask-SocketIO  WebSocket handshake Unexpected response code 404

WebSocket connection to ‘ws://127.0.0.1:8080/cmd/prog’ failed: Error during WebSocket handshake: Unexpected response code: 404 – 开源中国社区

failed: WebSocket is closed before the connection is established. –

->

Welcome to Flask-SocketIO’s documentation! — Flask-SocketIO documentation

此处,看到说是:

需要先安装:

eventlet或gevent

但是我此处没有安装啊:

alembic==0.8.8
aniso8601==1.1.0
click==6.6
croniter==0.3.12
enum34==1.1.6
Flask==0.11.1
Flask-HTTPAuth==3.2.1
Flask-Login==0.3.2
Flask-Migrate==2.0.0
Flask-Redis==0.3.0
Flask-RESTful==0.3.5
Flask-RQ2==16.1.1
Flask-Script==2.0.5
Flask-SocketIO==2.7.1
Flask-SQLAlchemy==2.1
gunicorn==19.6.0
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.4
MarkupSafe==0.23
meld3==1.0.2
MySQL-python==1.2.5
python-dateutil==2.5.3
python-editor==1.0.1
python-engineio==1.0.3
python-socketio==1.6.0
pytz==2016.6.1
redis==2.10.5
rq==0.6.0
rq-scheduler==0.7.0
six==1.10.0
SQLAlchemy==1.1.1
supervisor==3.3.1
Werkzeug==0.11.11

说是:

If neither of these are installed, then the application runs on Flask’s development web server, which is not appropriate for production use.

所以先去安装:

eventlet

不对,此处我的是:

gunicorn

作为webserver

还是好好看看整个文档吧。。。

以及另外作者写的帖子:

Easy WebSockets with Flask and Gevent – miguelgrinberg.com

“Note that Flask-SocketIO depends on gevent

This method starts gevent, the only supported web server. Using gunicorn with a gevent worker should also work. ”

安装gevent之前,先去看看

【整理】python中的gevent

Introduction — gevent 1.2a2.dev0 documentation

“gevent and greenlet can both be installed with pip, e.g., pip install gevent. On Windows and OS X, both gevent and greenlet are distributed as binary wheels,"

>

Python Wheels

Welcome to Flask-SocketIO’s documentation! — Flask-SocketIO documentation

“The extension automatically detects which asynchronous framework to use based on what is installed. Preference is given to eventlet, followed by gevent. For WebSocket support in gevent, uWSGI is preferred, followed by gevent-websocket. If neither eventlet nor gevent are installed, then the Flask development server is used."

Socket.IO · GitHub

-》

GitHub – socketio/socket.io-client-swift

GitHub – socketio/socket.io-client-java: Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.

(RunningFast) ➜  staging pip install gevent
Collecting gevent
  Downloading gevent-1.1.2-cp27-cp27m-manylinux1_x86_64.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 23kB/s 
Collecting greenlet>=0.4.9 (from gevent)
  Downloading greenlet-0.4.10-cp27-cp27m-manylinux1_x86_64.whl (41kB)
    100% |████████████████████████████████| 51kB 26kB/s 
Installing collected packages: greenlet, gevent
Successfully installed gevent-1.1.2 greenlet-0.4.10

gevent (1.1.2)

greenlet (0.4.10)

但是问题依旧

Flask-SocketIO  not run

python – Trouble with accessing flask-socketio session – Stack Overflow

Flask-SocketIO doesn’t work with eventlet · Issue #255 · miguelgrinberg/Flask-SocketIO · GitHub

去卸载掉eventlet试试?

结果说是找不到:

(RunningFast) ➜  staging pip uninstall eventlet
Cannot uninstall requirement eventlet, not installed

然后才注意到,此处没有安装eventlet:

(RunningFast) ➜  staging pip list
alembic (0.8.8)
aniso8601 (1.1.0)
click (6.6)
croniter (0.3.12)
enum34 (1.1.6)
Flask (0.11.1)
Flask-HTTPAuth (3.2.1)
Flask-Login (0.3.2)
Flask-Migrate (2.0.0)
Flask-Redis (0.3.0)
Flask-RESTful (0.3.5)
Flask-RQ2 (16.1.1)
Flask-Script (2.0.5)
Flask-SocketIO (2.7.1)
Flask-SQLAlchemy (2.1)
gevent (1.1.2)
greenlet (0.4.10)
gunicorn (19.6.0)
itsdangerous (0.24)
Jinja2 (2.8)
Mako (1.0.4)
MarkupSafe (0.23)
meld3 (1.0.2)
MySQL-python (1.2.5)
pip (8.1.2)
python-dateutil (2.5.3)
python-editor (1.0.1)
python-engineio (1.0.3)
python-socketio (1.6.0)
pytz (2016.6.1)
redis (2.10.5)
rq (0.6.0)
rq-scheduler (0.7.0)
setuptools (28.0.0)
six (1.10.0)
SQLAlchemy (1.1.1)
supervisor (3.3.1)
Werkzeug (0.11.11)
wheel (0.30.0a0)

只是安装了:

gevent (1.1.2)

greenlet (0.4.10)

Websocket transport not available when using eventlet with the gunicorn web server · Issue #153 · miguelgrinberg/Flask-SocketIO · GitHub

去安装:

gevent-websocket

试试

(RunningFast) ➜  staging pip install gevent-websocket
Collecting gevent-websocket
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f8527a8f710>: Failed to establish a new connection: [Errno 97] Address family not supported by protocol’,)’: /simple/gevent-websocket/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f8526309c10>: Failed to establish a new connection: [Errno 97] Address family not supported by protocol’,)’: /simple/gevent-websocket/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f8526309910>: Failed to establish a new connection: [Errno 97] Address family not supported by protocol’,)’: /simple/gevent-websocket/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f8526309450>: Failed to establish a new connection: [Errno 97] Address family not supported by protocol’,)’: /simple/gevent-websocket/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f8526309c50>: Failed to establish a new connection: [Errno 97] Address family not supported by protocol’,)’: /simple/gevent-websocket/
  Could not find a version that satisfies the requirement gevent-websocket (from versions: )
No matching distribution found for gevent-websocket

Flask-SocketIO needs your help! – miguelgrinberg.com

“The 0.x releases depended on gevent, gevent-socketio and gevent-websocket. In release 1.0 gevent-socketio and gevent-websocket are not used anymore, and gevent is one of three options for backend web server. The other two options are eventlet, and any regular multi-threaded WSGI server, including Flask’s development web server."

算了,去换用:

eventlet

先卸载:

gevent

(RunningFast) ➜  staging pip uninstall gevent
Uninstalling gevent-1.1.2:
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/DESCRIPTION.rst
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/INSTALLER
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/METADATA
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/RECORD
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/WHEEL
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/metadata.json
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent-1.1.2.dist-info/top_level.txt
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/__init__.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/__init__.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_corecffi.so
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_corecffi_build.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_corecffi_build.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_fileobjectcommon.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_fileobjectcommon.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_fileobjectposix.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_fileobjectposix.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_semaphore.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_semaphore.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_semaphore.so
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_socket2.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_socket2.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_socket3.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_socketcommon.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_socketcommon.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_ssl2.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_ssl2.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_ssl3.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_ssl3.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_sslgte279.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_sslgte279.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_tblib.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_tblib.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_threading.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_threading.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_util_py2.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/_util_py2.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/ares.so
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/backdoor.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/backdoor.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/baseserver.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/baseserver.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/builtins.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/builtins.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/core.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/core.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/corecext.so
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/corecffi.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/corecffi.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/coros.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/coros.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/event.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/event.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/fileobject.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/fileobject.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/greenlet.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/greenlet.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/hub.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/hub.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/local.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/local.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/lock.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/lock.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/monkey.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/monkey.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/os.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/os.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/pool.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/pool.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/pywsgi.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/pywsgi.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/queue.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/queue.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/resolver_ares.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/resolver_ares.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/resolver_thread.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/resolver_thread.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/select.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/select.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/server.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/server.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/signal.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/signal.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/socket.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/socket.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/ssl.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/ssl.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/subprocess.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/subprocess.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/thread.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/thread.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/threading.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/threading.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/threadpool.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/threadpool.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/timeout.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/timeout.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/util.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/util.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/win32util.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/win32util.pyc
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/wsgi.py
  /root/Envs/RunningFast/lib/python2.7/site-packages/gevent/wsgi.pyc
Proceed (y/n)? y
  Successfully uninstalled gevent-1.1.2

再去安装:

eventlet

(RunningFast) ➜  staging pip install eventlet
Collecting eventlet
  Downloading eventlet-0.19.0-py2.py3-none-any.whl (138kB)
    100% |████████████████████████████████| 143kB 16kB/s 
Requirement already satisfied (use –upgrade to upgrade): greenlet>=0.3 in /root/Envs/RunningFast/lib/python2.7/site-packages (from eventlet)
Installing collected packages: eventlet
Successfully installed eventlet-0.19.0

结果问题依旧。

websocket.org Echo Test – Powered by Kaazing

ERROR: undefined DISCONNECTED

Flask-SocketIO ERROR: undefined DISCONNECTED

Session disconnected issue · Issue #248 · miguelgrinberg/Flask-SocketIO · GitHub

加上log:

socketio.run(app, engineio_logger=True)

看看之后用测试工具访问:

ws://115.29.173.126:21085/test

是否有log输出

结果log中都没有engineio的log。。。

Socket.IO — Logging and Debugging

python – Flask-SocketIO server using polling instead of websockets – Stack Overflow

node.js – SocketIO when server is down the clientside gives a window alert of undefined? – Stack Overflow

然后去:

把Flask-SocketIO的代码都去掉

然后再去测试,结果错误的现象是一样的

Simple Web Socket Client test Flask-SocketIO

how test Flask-SocketIO

Welcome to Flask-SocketIO’s documentation! — Flask-SocketIO documentation

Flask-SocketIO/test_socketio.py at master · miguelgrinberg/Flask-SocketIO · GitHub

python – Testing Flask with Socketio and Flask-Login – Stack Overflow

Flask-SocketIO中实例代码的阅读笔记 – junanhonglei – 开源中国社区

flask-socketio

【总结】

最终,折腾了N久,还是没有解决此问题。

最终,换用了Flask-Sockets,而实现了websocket:

【已解决】使用Flask-Sockets去实现Flask中的Websocket

转载请注明:在路上 » 【未解决】Simple WebSocket Client测试Flask的websocket显示OPENING且弹出undefined对话框

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.204 seconds, using 20.26MB memory