使用 Flask-socketio 和 socketIO 客户端

Posted

技术标签:

【中文标题】使用 Flask-socketio 和 socketIO 客户端【英文标题】:Using Flask-socketio and the socketIO client 【发布时间】:2018-06-18 00:17:51 【问题描述】:

我目前正在尝试了解套接字的工作原理。我正在使用 Flask-socketio 和 python socketio 客户端并运行一个基本示例。这是我到目前为止所做的事情

app.py

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@socketio.on('aaa')
def test_connect():
    print("Welcome, aaa received")
    emit('aaa_response', 'data': 'Server')

if __name__ == '__main__':
    socketio.run(app, port=8000)

client.py

from socketIO_client import SocketIO, LoggingNamespace

def on_aaa_response(args):
    print('on_aaa_response', args['data'])

socketIO = SocketIO('localhost', 8000, LoggingNamespace)
socketIO.on('aaa_response', on_aaa_response)
socketIO.emit('aaa')
socketIO.wait(seconds=1)

运行client.py 时出现断言错误。我确实看到服务器打印"Welcome, aaa recived"。我不知道我在这里做错了什么,如果这里需要的是我的日志 错误日志

Exception in thread Thread-1:
Traceback (most recent call last):
  File "c:\users\dj\appdata\local\programs\python\python36\Lib\threading.py", li
ne 916, in _bootstrap_inner
    self.run()
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\heartbeats.py", line 27, in run
    self._send_heartbeat()
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 203, in _ping
    engineIO_packet_type, engineIO_packet_data)
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\transports.py", line 109, in send_packet
    assert response.content == b'ok'
AssertionError

Traceback (most recent call last):
  File "demo.py", line 8, in <module>
    socketIO.emit('aaa')
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 424, in emit
    self._message(str(socketIO_packet_type) + socketIO_packet_data)
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 33, in wrap
    return f(*args, **kw)
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 219, in _message
    transport.send_packet(engineIO_packet_type, engineIO_packet_data)
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\transports.py", line 109, in send_packet
    assert response.content == b'ok'
AssertionError
Exception ignored in: <bound method SocketIO.__del__ of <socketIO_client.SocketI
O object at 0x00000028079DC320>>
Traceback (most recent call last):
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 364, in __del__
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 400, in disconnect
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\__init__.py", line 193, in _close
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\transports.py", line 108, in send_packet
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\socketIO_c
lient\transports.py", line 191, in get_response
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\s
essions.py", line 555, in post
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\s
essions.py", line 494, in request
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\s
essions.py", line 419, in prepare_request
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\c
ookies.py", line 537, in merge_cookies
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\site-packages\requests\c
ookies.py", line 353, in update
  File "C:\Users\Dj\Desktop\Flask\FLASK-SOCKET\venv\lib\copy.py", line 96, in co
py
ImportError: sys.meta_path is None, Python is likely shutting down

【问题讨论】:

您复制的堆栈跟踪在右侧缺少一些内容,能否请您包含完整的堆栈跟踪?此外,如果将 seconds 参数更改为 socketIO.wait() 到 5 秒而不是 1 秒,是否会发生任何变化? 感谢您回复@Miguel,我已更新错误日志。并且更改 socketIO.wait(seconds=5) 会产生完全相同的错误。 可以查看与GUI客户端firecamp.app的连接 我正在尝试使用类似的东西。如果服务器没有运行(flask_socketio)(通常服务器关闭/断开连接),客户端是否有办法超时,现在我的客户端只是等待服务器,如果没有可用的连接,我希望它返回。 【参考方案1】:

根据堆栈跟踪,我无法识别您正在使用的 socketIO-client 软件包的版本。它似乎不是当前的。

我在这里测试了您的两个应用程序,它们似乎与客户端的 0.7.2 版本完美配合。我建议你运行pip install --upgrade socketIO-client==0.7.2,然后再试一次。

【讨论】:

谢谢。现在可以了。我其实用的是这个版本pypi.python.org/pypi/socketIO-client-2/0.7.4pip install socketIO-client-2 以防万一其他人遇到此问题,socketIO-client-2 不起作用,请使用pypi.python.org/pypi/socketIO-client

以上是关于使用 Flask-socketio 和 socketIO 客户端的主要内容,如果未能解决你的问题,请参考以下文章

使用flask和flask-socketio配置nginx、uwsgi

[翻译] flask-SocketIO

Flask-SocketIO 未使用 Gevent/Gevent-websocket

flask-SocketIO 和 eventlet 有问题

Flask-SocketIO发送图像

Flask-socketio,向另一个命名空间发出事件