为什么数据在socket.io?

时间:2016-02-19 20:38:20

标签: javascript python-2.7 socket.io flask-socketio

我正在建立一个小应用程序来记录日志并在客户端上显示它。但是,当我将行添加到日志中时,我会丢失一些数据。

这是相关的Python处理程序:

@socketio.on('Request logs')
def handle_request_logs():
    logfile = '/path/to/some_log.log'

    # gets last 25 lines from logfile
    last_n_lines = tailer.tail(open(logfile), 25)

    # sends the initial 25 lines
    emit('Initial send', { 'lines' : last_n_lines })

    # emits lines that were appended to logfile
    @copy_current_request_context
    def tail(logfile):
        for line in tailer.follow(open(logfile)):
            print("About to emit {0}".format(line))
            emit('log', { 'line' : line })

    # emit added lines as they come
    t = threading.Thread(target=tail, args=(logfile,))
    t.start()

这是接收'log'的JS:

  socket.on('log', function(data) {
      alert("Got some data: ", data['line']);
  });

每当我向日志添加内容(例如echo 'hello, world!' >> /path/to/some_log.log)时,我会在客户端看到一条带有消息的警报 "Got some data: "。但是,我的服务器打印"About to emit hello, world!"

为什么会这样?

1 个答案:

答案 0 :(得分:1)

你的警报(javascript)中有一个逗号。它应该是一个+用于连接。