简体   繁体   English

清除节点流内部缓冲区

[英]Clearing node stream internal buffer

I am using Node Serialport to set an event listener and I am then checking to see if the data that is being streamed over the connection has the correct units. 我正在使用Node Serialport设置事件侦听器,然后检查是否通过连接流传输的数据是否具有正确的单位。 If these units do not match the user's set units, I am using the .pause() method and notifying the user that the device is not transmitting the correct units. 如果这些单位与用户设置的单位不匹配,我将使用.pause()方法并通知用户该设备未传输正确的单位。 The user can then press a "Resume" button which calls the .resume() method. 然后,用户可以按下“ Resume”按钮,该按钮将调用.resume()方法。

According to the Node docs: "The readable.pause() method will cause a stream in flowing mode to stop emitting 'data' events, switching out of flowing mode. Any data that becomes available will remain in the internal buffer." 根据Node docs的说法:“可读.pause()方法将导致处于流模式的流停止发出'数据'事件,切换为流模式。任何可用的数据将保留在内部缓冲区中。”

The problem seems to be that I need to clear this internal buffer. 问题似乎是我需要清除此内部缓冲区。 As it currently is, I need to call .resume() many times to go through all the buffered data which has the wrong units. 就目前而言,我需要多次调用.resume()以遍历所有具有错误单位的缓冲数据。

Here is a general idea of how the code is set up: 这是代码设置的一般思路:

//Listen for data events and pipe them through a parser.
path.pipe(new Readline({ delimiter: '\r\n' })).on('data', data => {
  //do something with data
})

//Then if data units !== set units, call path.pause()
// User can press "Resume" once the device's units are changed.
<button onClick={() => path.flush(() => path.resume())}>Resume</button>

How can I clear the internal buffer or is there an alternative to .pause() that avoids this problem entirely without closing and then re-opening the connection? 如何清除内部缓冲区,或者.pause()是否有替代方法,可以完全避免此问题而无需关闭然后重新打开连接?

It sounds as if you just want to unpipe the source: 听起来好像只想解开源代码:

 source.unpipe(target);

that way, all the data is streaming into nowhere, then to resume somewhen just 这样,所有数据都将流向无处,然后在刚刚恢复

source.pipe(target);

again. 再次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确清除节点中可读流的内部缓冲区? - How to properly clear the internal buffer of a readable stream in node? 清除 MIDI 输出缓冲区 - Clearing MIDI output buffer Node.js Stream - Buffer to String给出[object Object] - Node.js Stream - Buffer to String gives [object Object] 节点:尝试流式传输 Excel 文件并将缓冲区传递给“xlsx”库 - Node: trying to stream an Excel file and pass the buffer to 'xlsx' library 节点 JS:错误:在 TCP.onStreamRead 读取 ECONNRESET(节点:内部/stream_base_commons:211:20) - Node JS: Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:211:20) buffer 和 stream - 它们有什么关系? - buffer and stream - how are they related? 暂停和缓冲流 - gulp pause and buffer stream 如何在不转换为字符串的情况下将缓冲区直接写入 Node.js 中的 websocket-stream? - How can I write a Buffer directly to websocket-stream in Node.js without converting to string? RxJs 缓冲区在关闭通知器发出时不清除缓冲区 - RxJs buffer without clearing the buffer when closingNotifier emits 节点 js 在 TCP.onStreamRead 处读取 ECONNRESET (internal/stream_base_commons.js:209:20) | 网络套接字 - Node js read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:209:20) | Websocket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM