简体   繁体   English

如何在套接字Node.js中检测网络故障

[英]How to detect network failure in sockets Node.js

I am trying to write internal transport system. 我正在尝试编写内部传输系统。 Data should be transferred from client to server using net sockets. 数据应该使用网络套接字从客户端传输到服务器。 It is working fine except handling of network issues. 除处理网络问题外,它工作正常。 If I place firewall between client and server, on both sides I will not see any error, so data will continue to fill kernel buffer on client side. 如果在客户端和服务器之间放置防火墙,则在两侧都不会看到任何错误,因此数据将继续填充客户端的内核缓冲区。 And if I will restart app in this moment I will lose all data in buffer. 如果我此时重启应用程序,我将丢失缓冲区中的所有数据。

Question: Do we have any way to detect network issues? 问题:我们有什么方法可以检测网络问题? Do we have any way to get data back from kernel buffers? 我们是否有办法从内核缓冲区取回数据?

Node js exposes the low level socket api to you very directly. Node js非常直接地向您公开了低级套接字API。 I'm assuming that you are using a TCP socket to send and receive data. 我假设您正在使用TCP套接字发送和接收数据。

One way to ensure that there is an active connection between the client and server is to send heartbeat signals back and forth. 确保客户端和服务器之间存在活动连接的一种方法是来回发送心跳信号。 If you fail to receive a heartbeat from the server while sending data, you can assume that the connection failed. 如果在发送数据时未能从服务器接收到心跳,则可以认为连接失败。

As for the second part of your question: There is no easy way to get data back from kernel buffers. 至于问题的第二部分:没有简单的方法可以从内核缓冲区取回数据。 If losing the data will be a problem, I would make sure to write it to disk. 如果丢失数据将是一个问题,我将确保将其写入磁盘。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM