client could not receive emits from the server.
See original GitHub issuei am learning flask now and i have a big problem. i use flask and flask-scoketio. my client could not receive emits from the server. i dont know why. the websocket is Upgrade
(10728) accepted ('127.0.0.1', 52781)
(10728) accepted ('127.0.0.1', 52782)
127.0.0.1 - - [01/Mar/2017 16:14:38] "GET / HTTP/1.1" 200 5263 0.010000
127.0.0.1 - - [01/Mar/2017 16:14:38] "GET /static/web.css HTTP/1.1" 200 6780 0.219000
111c03a0438947ecae776c4103091c37: Sending packet OPEN data {'pingInterval': 25000, 'pingTimeout': 60000, 'upgrades': ['websocket'], 'sid': '111c03a0438947ecae776c4103091c37'}
111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 0
127.0.0.1 - - [01/Mar/2017 16:14:38] "GET /socket.io/?EIO=3&transport=polling&t=Lg8xtLu HTTP/1.1" 200 381 0.001000
(10728) accepted ('127.0.0.1', 52784)
111c03a0438947ecae776c4103091c37: Received request to upgrade to websocket
111c03a0438947ecae776c4103091c37: Sending packet NOOP data None
111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"hello"}]
111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":"welcome"}]
127.0.0.1 - - [01/Mar/2017 16:14:38] "POST /socket.io/?EIO=3&transport=polling&t=Lg8xtM9&sid=111c03a0438947ecae776c4103091c37 HTTP/1.1" 200 219 0.002000
127.0.0.1 - - [01/Mar/2017 16:14:38] "GET /socket.io/?EIO=3&transport=polling&t=Lg8xtMA&sid=111c03a0438947ecae776c4103091c37 HTTP/1.1" 200 260 0.000000
(10728) accepted ('127.0.0.1', 52785)
127.0.0.1 - - [01/Mar/2017 16:14:38] "GET /favicon.ico HTTP/1.1" 404 366 0.000000
111c03a0438947ecae776c4103091c37: Upgrade to websocket successful
when i click in client,the emits is empty
111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"one"}] 111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":""}] 111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"two"}] 111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":""}] 111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"three"}] 111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":""}] 111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"four"}] 111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":""}] 111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"five"}] 111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":""}] 111c03a0438947ecae776c4103091c37: Received packet MESSAGE data 2["message",{"data":"six"}] 111c03a0438947ecae776c4103091c37: Sending packet MESSAGE data 2["server_response",{"data":""}]
here is the code
server:
@socketio.on('message')
def connectevent(job_name):
if job_name['data'] == 'hello':
emit('server_response', {'data': 'welcome'})
else:
log_command = ['ping','-t','192.168.4.1']
log_out = subprocess.Popen(log_command, stdout=subprocess.PIPE)
while log_out:
out = log_out.stdout.readline().strip()
emit('server_response', {'data':out})
the out could not print fully and the emits could not send,it only print
111c03a0438947ecae776c4103091c37: Received packet PING data None
111c03a0438947ecae776c4103091c37: Sending packet PONG data None
111c03a0438947ecae776c4103091c37: Received packet PING data None
111c03a0438947ecae776c4103091c37: Sending packet PONG data None
111c03a0438947ecae776c4103091c37: Received packet PING data None
111c03a0438947ecae776c4103091c37: Sending packet PONG data None
111c03a0438947ecae776c4103091c37: Received packet PING data None
111c03a0438947ecae776c4103091c37: Sending packet PONG data None
if i replace while with if , it works,but it only send one line to client.
client
var socket = io.connect('http://' + document.domain + ':' + location.port);
var name = 'hello'
function jobname(name){
socket.emit('message',{'data':name});
return false;
};
socket.on('connect', function() {
socket.emit('message',{'data':name});
})
socket.on('server_response', function(msgg) {
console.log(msgg)
$('#loog').append('<br>' + $('<div/>').text(msgg.data).html());
});
thanks everyone
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (4 by maintainers)
@kyouko-taiga you can’t use regular networking and threading functions when you use eventlet, because those are blocking. Your best shot is to see if monkey patching resolves the problem. Just add the following as the first lines of your script (above all your other imports):
Are you using eventlet or gevent? If you are, add a
socketio.sleep(0)
after theemit
in your loop. That will release the CPU and let other tasks do their work.