为什么数据在 socket.io 中丢失?
Why is data being lost in socket.io?
我正在构建一个小型应用程序,用于跟踪日志并将其显示在客户端上。但是,当我将行附加到我的日志时,我丢失了一些数据。
这是相关的 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)。它可能应该是一个 + 用于连接。
我正在构建一个小型应用程序,用于跟踪日志并将其显示在客户端上。但是,当我将行附加到我的日志时,我丢失了一些数据。
这是相关的 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'
:
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)。它可能应该是一个 + 用于连接。