Issue
I have Flask server, which handles HTTP requests but also WebSocket using flask-socketio
. Code of server here:
from flask_socketio import SocketIO, emit
from flask import Flask
app = Flask(__name__)
socketio = SocketIO(app)
@socketio.on('response')
def message(data):
time.sleep(1)
emit('sensor', {"data": "hello"})
@socketio.on('connect')
def connect():
emit('after connect')
if __name__ == '__main__':
socketio.run(app)
and script tag in html looks like this:
<script>
var socket = io("ws://192.168.100.3:5000");
socket.on('after connect', function(msg) {
console.log('connected')
socket.emit('response', msg)
});
socket.on('sensor', function(msg) {
console.log(msg.data)
socket.emit('response', msg)
});
</script>
Everything works, I got the communication, but one thing that concerning me is that in terminal where server run I get code 200 for GET and POST requests.
192.168.100.10 - - [17/Apr/2022 11:36:27] "GET /socket.io/?EIO=2&transport=polling&t=1650191787548-1094&sid=faed6806df0a437d8d17bd8a294f8754 HTTP/1.1" 200 -
192.168.100.10 - - [17/Apr/2022 11:36:27] "POST /socket.io/?EIO=2&transport=polling&t=1650191788570-1095&sid=faed6806df0a437d8d17bd8a294f8754 HTTP/1.1" 200 -
I get response in console I think communication is right. But with those request that I am "making" every second resources usage in web browser is getting bigger. Small amount but still. As far as I know WebSockets should not send HTTP requests to server, but it has to be TCP/IP constant connection.
What am I doing wrong, or it is my WebSockets understanding wrong?
Solution
The GET and POST requests are there because you are using Socket.IO. As stated in its documentation, Socket.IO starts with HTTP long-polling before it attempts to establish a WebSocket connection. It will also fallback to HTTP long-polling if it cannot use WebSocket.
Answered By - Krerkkiat Chusap
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.