[英]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.