本人是新手,第一次尝试写个简单功能,已经被折腾好几天了
我用 flask 框架, flask-socketio 。写了一个服务器端实时传送数据展示在前端的程序,但是问题是,前端无法收到后端发送的数据,全部阻塞在服务器上了
前端也无法收到后端的数据,但是我的 websocket 管道已经建立
5d81f64ec0d14cc5a5a70c075caaefdd: Received request to upgrade to websocket
5d81f64ec0d14cc5a5a70c075caaefdd: Received packet MESSAGE data 2["message",{"data":"hello"}]
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet MESSAGE data 2["server_response",{"data":"welcome"}]
127.0.0.1 - - [01/Mar/2017 15:19:35] "POST /socket.io/?EIO=3&transport=polling&t=Lg8lGsx&sid=5d81f64ec0d14cc5a5a70c075caaefdd HTTP/1.1" 200 219 0.001000
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet NOOP data None
127.0.0.1 - - [01/Mar/2017 15:19:35] "GET /socket.io/?EIO=3&transport=polling&t=Lg8lGsx.0&sid=5d81f64ec0d14cc5a5a70c075caaefdd HTTP/1.1" 200 260 0.000000
5d81f64ec0d14cc5a5a70c075caaefdd: Upgrade to websocket successful
想问一下 各位大神,我的问题出在哪里,有没有什么好的解决办法。
前端触发 onclick 后,发送的全是空:
5d81f64ec0d14cc5a5a70c075caaefdd: Received packet MESSAGE data 2["message",{"data":"wawawa"}]
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet MESSAGE data 2["server_response",{"data":""}]
5d81f64ec0d14cc5a5a70c075caaefdd: Received packet MESSAGE data 2["message",{"data":"aaaaa"}]
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet MESSAGE data 2["server_response",{"data":""}]
5d81f64ec0d14cc5a5a70c075caaefdd: Received packet MESSAGE data 2["message",{"data":"dsdaaf"}]
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet MESSAGE data 2["server_response",{"data":""}]
5d81f64ec0d14cc5a5a70c075caaefdd: Received packet MESSAGE data 2["message",{"data":"ddddwdad"}]
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet MESSAGE data 2["server_response",{"data":""}]
5d81f64ec0d14cc5a5a70c075caaefdd: Received packet MESSAGE data 2["message",{"data":"adadeeel"}]
5d81f64ec0d14cc5a5a70c075caaefdd: Sending packet MESSAGE data 2["server_response",{"data":""}]
代码:
服务端:
@
socketio.on('connect_event')
def connectevent(job_name):
if job_name['data'] == 'hello':
emit('serverresponse',{'data':'welcome'})
else:
log_command = ['tailf','/var/log/messages']
log_out = subprocess.Popen(log_command,stdout=subprocess.PIPE)
while log_out:
out = log_out.stdout.readline().strip()
emit('serverresponse',{'data':out})
while 里 print out ,是可以打印出来的,但是 emit 的时候却无法发送给前端
前端:
var socket = io.connect('http://' + document.domain + ':' + location.port);
var name = 'hello'
function jobname(name){
socket.emit('connect_event',{'data':name});
return false;
};
socket.on('connect', function() {
socket.emit('connect_event',{'data':name});
})
socket.on('serverresponse', function(msgg) {
console.log(msgg)
$('#loog').append('<br>' + $('<div/>').text(msgg.data).html());
});