无法将超过一定长度的消息字符串发送到服务器 Flask-SocketIO

Unable to send message string more than a certain length to server Flask-SocketIO

描述

我正在创建一个简单的应用程序,它从浏览器客户端脚本中获取网络摄像头流,将每个帧转换为字符串格式并通过由 Flask-SocketIO 组成的后端服务器发送。问题是,当字符串长度超过一定长度时,后端不会收到消息,我会以每 5 秒 1 条消息的速率谈论最多 60 kb 的字符串,只是为了测试连接。

index.js(客户端)

    <div id="container">
   <canvas id="canvasOutput"></canvas>
   <video autoplay="true" id="videoElement"></video>
</div>
<div class = 'video'>
   <img id="image">
</div>
<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/4.1.2/socket.io.js" integrity="sha512-iZIBSs+gDyTH0ZhUem9eQ1t4DcEn2B9lHxfRMeGQhyNdSUz+rb+5A3ummX6DQTOIs1XK0gOteOg/LPtSo9VJ+w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="text/javascript" charset="utf-8">
   $(document).ready(function() {
   
   var socket = io('http://localhost:5000');
   
   socket.on('connect', function(){
       console.log("Connected...!", socket.connected)
   });
   
   const video = document.querySelector("#videoElement");

   setInterval(() => {
   
       *some frame transformation logic*
    
       socket.emit('image', data);
   
   }, 5000);
   
   
   socket.on('response_back', function(image){
       const image_id = document.getElementById('image');
       image_id.src = image;
   
   });
   });
</script>

app.py(服务器端)

*importing libraries*

app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*", logger=True)

@app.route('/', methods=['POST', 'GET'])
def index():
    return render_template('index.html')

@socketio.on('image')
def image(data_image):
    sbuf = io.StringIO()
    sbuf.write(data_image)  
    *image transformation code*
    emit('response_back', stringData)
    
if __name__ == '__main__':
    socketio.run(app, host='127.0.0.1', debug=True)

服务器日志

C:\Python39\python39.exe "C:/Users/Talal Zahid/Desktop/Idenfo/Liveness Detection/webjs_flask_socket/app.py"
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 845-107-437
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [06/Jul/2021 05:53:53] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [06/Jul/2021 05:53:54] "GET /socket.io/?EIO=4&transport=polling&t=NfvJg1O HTTP/1.1" 200 -
127.0.0.1 - - [06/Jul/2021 05:53:54] "POST /socket.io/?EIO=4&transport=polling&t=NfvJg6g&sid=ss6f4I3fOcnsJQY4AAAA HTTP/1.1" 200 -
127.0.0.1 - - [06/Jul/2021 05:53:54] "GET /socket.io/?EIO=4&transport=polling&t=NfvJg6h&sid=ss6f4I3fOcnsJQY4AAAA HTTP/1.1" 200 -
127.0.0.1 - - [06/Jul/2021 05:53:54] "GET /socket.io/?EIO=4&transport=polling&t=NfvJgBX&sid=ss6f4I3fOcnsJQY4AAAA HTTP/1.1" 200 -
message handler error
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\engineio\server.py", line 603, in _trigger_event
    return self.handlers[event](*args)
  File "C:\Python39\lib\site-packages\socketio\server.py", line 740, in _handle_eio_message
    pkt = packet.Packet(encoded_packet=data)
  File "C:\Python39\lib\site-packages\socketio\packet.py", line 40, in __init__
    self.attachment_count = self.decode(encoded_packet)
  File "C:\Python39\lib\site-packages\socketio\packet.py", line 111, in decode
    self.data = self.json.loads(ep)
  File "C:\Python39\lib\site-packages\engineio\json.py", line 16, in loads
    return original_loads(*args, **kwargs)
  File "C:\Python39\lib\json\__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "C:\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python39\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 10 (char 9)
127.0.0.1 - - [06/Jul/2021 05:54:03] "GET /socket.io/?EIO=4&transport=websocket&sid=8sxFs9IeZyAmGn1XAAAC HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Python39\Lib\site-packages\flask\app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python39\Lib\site-packages\flask_socketio\__init__.py", line 45, in __call__
    return super(_SocketIOMiddleware, self).__call__(environ,
  File "C:\Python39\Lib\site-packages\engineio\middleware.py", line 60, in __call__
    return self.engineio_app.handle_request(environ, start_response)
  File "C:\Python39\Lib\site-packages\socketio\server.py", line 573, in handle_request
    return self.eio.handle_request(environ, start_response)
  File "C:\Python39\Lib\site-packages\engineio\server.py", line 392, in handle_request
    packets = socket.handle_get_request(
  File "C:\Python39\Lib\site-packages\engineio\socket.py", line 103, in handle_get_request
    return getattr(self, '_upgrade_' + transport)(environ,
  File "C:\Python39\Lib\site-packages\engineio\socket.py", line 158, in _upgrade_websocket
    return ws(environ, start_response)
  File "C:\Python39\Lib\site-packages\engineio\async_drivers\threading.py", line 23, in __call__
    return self.app(self)
  File "C:\Python39\Lib\site-packages\engineio\socket.py", line 236, in _websocket_handler
    pkt = packet.Packet(encoded_packet=p)
  File "C:\Python39\Lib\site-packages\engineio\packet.py", line 27, in __init__
    self.decode(encoded_packet)
  File "C:\Python39\Lib\site-packages\engineio\packet.py", line 63, in decode
    self.packet_type = int(encoded_packet[0])
ValueError: invalid literal for int() with base 10: 'm'
Exception in thread Thread-17:
Traceback (most recent call last):
  File "C:\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python39\lib\site-packages\simple_websocket\ws.py", line 90, in _thread
    self.connected = self._handle_events()
  File "C:\Python39\lib\site-packages\simple_websocket\ws.py", line 117, in _handle_events
    self.sock.send(out_data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

客户端日志

其他上下文

我是 运行 一台 windows 机器。字符串长度似乎是导致问题的原因,就好像我发送任何大于 x 字节的字符串一样,我得到了同样的错误。

这是 GitHub 上包 simple-websocket which is a indirect dependency of the Flask-SocketIO package. It has since been fixed by its owner Miguel Grinberg. To get this fix, simple upgrade your simple-websocket (which you already have installed as shown in the error trace) to the version that's on the main branch here 中发生的错误。