为什么数据在 socket.io 中丢失

Why is data being lost in socket.io?

本文关键字:io socket 数据 为什么      更新时间:2023-09-26

我正在构建一个小应用程序,它跟踪日志并将其显示在客户端上。但是,当我将行附加到日志时,我会丢失一些数据。

以下是相关的 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!"

为什么会这样?

警报中有一个逗号(javascript)。 它可能应该是 + 表示串联。