1、版本问题
问题解决
根据官方给定的兼容版本,从socket.io官网CDN下载最新的4.4.1版本js文件,https://cdn.socket.io/。
python-engineio使用版本。需要更新的javascript.socketio包,具体可对照官方文档Requirements部分末尾
https://flask-socketio.readthedocs.io/en/latest/intro.html#requirements
前端加入
还有Flask和Werkzeug冲突
1、版本1(低版本推荐)
解决
以我本机为例,运行下列命令
pip3 install --upgrade Flask-SocketIO==4.3.1
pip3 install --upgrade python-engineio==3.13.2
pip3 install --upgrade python-socketio==4.6.0
如果出现
ImportError: cannot import name ‘run_with_reloader’ from ‘werkzeug.serving’
或
ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’
则运行下面命令
pip3 install --upgrade flask==1.1.4
pip3 install --upgrade Werkzeug==1.0.1
pip3 install --upgrade itsdangerous==1.1.0
pip3 install --upgrade Jinja2==2.11.2
pip3 install --upgrade MarkupSafe==2.0.1
参考链接:https://blog.csdn.net/xiru9972/article/details/125127955
bidict==0.22.1
blinker==1.6.2
cachelib==0.10.2
cffi==1.15.1
click==7.1.2
colorama==0.4.6
coverage==7.2.7
dnspython==2.3.0
eventlet==0.33.3
Flask==1.1.4
Flask-Cors==3.0.10
Flask-SocketIO==5.2.0
gevent==22.10.2
gevent-websocket==0.10.1
greenlet==2.0.2
h11==0.14.0
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==2.0.1
pycparser==2.21
python-engineio==4.4.1
python-socketio==5.8.0
simple-websocket==0.10.1
six==1.16.0
websocket==0.2.1
Werkzeug==1.0.1
wsproto==1.2.0
zope.event==4.6
zope.interface==6.0
前端:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js" integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ==" crossorigin="anonymous"></script>
2、版本2
Flask==2.2.2
Flask-SocketIO==5.2.0
MarkupSafe==2.1.1
Werkzeug==2.2.2
pip install simple-websocket
前端:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js" integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ==" crossorigin="anonymous"></script>
3、版本3
Flask-SocketIO==4.3.1
python-engineio==3.13.2
python-socketio==4.6.0
Flask==2.0.3
Werkzeug==2.0.3
pip install gevent-websocket gevent eventlet
前端
<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
bidict==0.22.1
blinker==1.6.2
cffi==1.15.1
click==8.1.3
colorama @ file:///C:/b/abs_a9ozq0l032/croot/colorama_1672387194846/work
coverage==7.2.7
dnspython==2.3.0
eventlet==0.33.3
Flask==2.0.3
Flask-Cors==3.0.10
Flask-SocketIO==4.3.1
gevent==22.10.2
gevent-websocket==0.10.1
greenlet==2.0.2
h==0.99
h11==0.14.0
itsdangerous==2.1.2
Jinja2 @ file:///C:/b/abs_7cdis66kl9/croot/jinja2_1666908141852/work
MarkupSafe @ file:///C:/ci/markupsafe_1654508036328/work
pycparser==2.21
python-engineio==3.13.2
python-socketio==4.6.0
simple-websocket==0.10.1
six==1.16.0
Werkzeug==2.0.3
wsproto==1.2.0
zope.event==4.6
zope.interface==6.0
新版本
bidict==0.22.1
certifi==2023.5.7
cffi==1.15.1
charset-normalizer==3.1.0
click==8.1.3
colorama==0.4.6
dnspython==2.3.0
eventlet==0.33.3
Flask==2.2.2
Flask-SocketIO==5.2.0
gevent==22.10.2
greenlet==2.0.2
h11==0.14.0
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
pip==23.1.2
pycparser==2.21
python-engineio==4.3.3
python-socketio==5.7.1
requests==2.31.0
setuptools==67.8.0
simple-websocket==0.7.0
six==1.16.0
urllib3==2.0.3
Werkzeug==2.2.2
wheel==0.38.4
wsproto==1.2.0
zope.event==4.6
zope.interface==6.0
2、跨域问题
在实例化SocketIO时,加上 cors_allowed_origins=""
#解决跨域问题
socketio.init_app(app, cors_allowed_origins='')