新版本flask-socketio的使用

现在网上很多flask-socketio的教程都过时了,今晚遇到了几个坑,花了一晚上解决

  1. Vue中使用socketio,flask用flask-socketio 报400错误
    现在最新的版本(2019年10月4日,版本4.2.1)中
    创建时需将
socketio = SocketIO(app)

改成

socketio = SocketIO(app, cors_allowed_origins='*')

这样可以支持跨域, 就不会报400错误

2.出现如此错误

Traceback (most recent call last):
  File "F:\OneDrive\PycharmProjects\test_socketio\wsgi.py", line 18, in 
    from flask_socketio import SocketIO
  File "g:\pipenv_venv\test_socketio-nqz-baho\lib\site-packages\flask_socketio\__init__.py", line 9, in 
    from socketio import socketio_manage
  File "g:\pipenv_venv\test_socketio-nqz-baho\lib\site-packages\socketio\__init__.py", line 3, in 
    from .client import Client
  File "g:\pipenv_venv\test_socketio-nqz-baho\lib\site-packages\socketio\client.py", line 6, in 
    import engineio
  File "g:\pipenv_venv\test_socketio-nqz-baho\lib\site-packages\engineio\__init__.py", line 3, in 
    from .client import Client
  File "g:\pipenv_venv\test_socketio-nqz-baho\lib\site-packages\engineio\client.py", line 44, in 
    original_signal_handler = signal.signal(signal.SIGINT, signal_handler)
  File "D:\Program Files\Python37\Lib\signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread

查阅此repo的issue得知, 在3.2.0后不支持使用flask run来启动(eventlet,gevent)项目,只能socketio.run(app)来启动
因此以后可以以 python app.py 的方式启动程序
在工厂函数创建后

app = create_app()
if __name__ == '__main__':
    socketio.run(app)

你可能感兴趣的:(新版本flask-socketio的使用)