Python 服务端实时持续发送数据给前端无法收到。新人求救!大问题!!

2017-03-01 15:28:02 +08:00
 Reloading
本人是新手,第一次尝试写个简单功能,已经被折腾好几天了
我用 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());
});
3225 次点击
所在节点    Python
2 条回复
jianmingforpc
2017-03-02 13:03:31 +08:00
=。=吓得刚准备用 flask-socketio 的我立马去试
Reloading
2017-03-08 14:53:28 +08:00
搞定了,
emit 后面加 socketio.sleep(0)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/344093

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX