V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Reloading
V2EX  ›  Python

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

  •  
  •   Reloading · 2017-03-01 15:28:02 +08:00 · 3243 次点击
    这是一个创建于 2850 天前的主题,其中的信息可能已经有所发展或是发生改变。
    本人是新手,第一次尝试写个简单功能,已经被折腾好几天了
    我用 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());
    });
    2 条回复    2017-03-08 14:53:28 +08:00
    jianmingforpc
        1
    jianmingforpc  
       2017-03-02 13:03:31 +08:00
    =。=吓得刚准备用 flask-socketio 的我立马去试
    Reloading
        2
    Reloading  
    OP
       2017-03-08 14:53:28 +08:00
    搞定了,
    emit 后面加 socketio.sleep(0)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2892 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:45 · PVG 22:45 · LAX 06:45 · JFK 09:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.