I am using flask-socketio to make a socket connection from my python web server to the javascript client. I am able to establish the connection but it breaks in a while(5 seconds or so) with the error
socket.io.min.js:2 WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=3&transport=websocket&sid=8ed663e14c6f47328b64f2d29a30d1cd' failed: Received a broken close frame containing invalid UTF-8.
Server side code to send the message(calling this periodically, say every 5 seconds)
def send_message(result):
# it will forward the message to all clients.
print("sending message")
socketio.send("Send working", json=False)
Client side code to receive the message
socket.on('message', function (data) {
console.log('message form backend ' + data);
});
Somehow it breaks -> then nothing happens for a while -> then connect again automatically -> then breaks again.
Can someone please help? Thanks a lot!
Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.
Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).
js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.
I fixed it.
socketio = SocketIO(app,ping_timeout=5)
ping_timeout – The time in seconds that the client waits for the server to respond before disconnecting. So it will be disconnected after 5 seconds if you do nothing.
The solution is that: Make your client side send message to server before timeout.
Cause my server usually sends data, so I make my client side like:
socket.on('message', function (data) {
console.log('message form backend ' + data);
socket.send('data receive!');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With