【问题标题】:NodeJS TCP socket often listen to END eventNodeJS TCP socket 经常监听 END 事件
【发布时间】:2021-04-20 10:05:02
【问题描述】:

我们有一个 Nodejs TCP 连接应用程序。它的作用是接受来自 IOT 设备的数据(通过 SIM 发送的数据)。该应用程序驻留在 Ubuntu 18 机器中。软件和物联网设备在同一区域时可以完美运行。例如:

服务器在美国,物联网设备在美国

服务器在澳大利亚,IOT设备在澳大利亚

此方案完美运行。但是,如果服务器或 IOT 设备位于不同的区域,则应用程序通常会监听 Socket 结束事件。例如:

服务器在美国 物联网设备在西班牙

服务器在美国 物联网设备在俄罗斯

在上面的例子中,应用程序经常监听套接字 END 事件,因此,设备总是以毫秒的差异重新连接。

然后我在 Google 上冲浪,许多人建议微调 Linux 默认 TCP 值。 https://www.cspsprotocol.com/tcp-keep-alive/ 。然后我更改了默认值,显然它仍然没有生效。当前值为

net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 50
net.ipv4.tcp_keepalive_time = 7200

作为第二步,我在我的 Nodejs 应用程序中添加了以下行,但仍然没有成功。

socket.setKeepAlive(true, 300000); 

这是我的 TCP 连接代码:

const Network = require("net");
const Events = require("events");
const { Signale } = require("signale");


const { PORT = 5252, HOST = "localhost" } = process.env;

const emitter = new Events.EventEmitter();

const logger = new Signale({
    scope: "server"
});

const server = Network.createServer(onClientConnected);

server.on("error", error => {
    if (error.code === "EADDRINUSE") {
        logger.warn(`Port is already in use, retrying ..`);
        setTimeout(() => {
            server.close();
            server.listen(PORT, () => {
                logger.success("Retrying listening to port", PORT, "on host", HOST);
            });
        }, 1000);
    }
});

let deviceConnectTimes = {};

function onClientConnected(socket) {
    const clientName = `${socket.remoteAddress}`;
    const networkLogger = logger.scope(clientName);

    socket.deviceMetadata = null;

    const TIMEOUT_IN_SECONDS = 30;
    socket.setTimeout(TIMEOUT_IN_SECONDS * 1000);

    socket.on("timeout", () => {
        networkLogger.warn(`The socket for ${clientName} has timed out.`);
        socket.end();
    });

    socket.on("error", error => {
        networkLogger.error(error);
    });

    socket.on("end", () => {
        networkLogger.warn(`The socket for ${clientName} has closed or timed out.`);
        console.log("socket end", socket.deviceMetadata);
    });

    socket.on("data", async buffer => {
        networkLogger.debug(buffer.toString("hex"));

        **....More code here**

    });
    socket.setKeepAlive(true, 300000); // New code added to solve the disconnetion issue
}

module.exports.events = emitter;

module.exports.listen = () => {
    server.listen(PORT, () => {
        logger.success("Listening on port", PORT, "on host", HOST);
    });
};

如果设备或服务器位于不同的国家/地区,任何帮助将不胜感激。

【问题讨论】:

    标签: node.js sockets ubuntu tcp keep-alive


    【解决方案1】:

    简单来说,有两种可能性:1) 纯粹是由延迟引起的,或者 2) 不是。

    所以我首先尝试在测试环境中模拟延迟的增加(理想情况下是 localhost 或简单的 LAN - 我有一个古老的 10-base-T 集线器,非常适合这种事情)。从基本设置开始,然后使用 tc 之类的东西添加延迟

    https://bencane.com/2012/07/16/tc-adding-simulated-network-latency-to-your-linux-server/

    如果您可以在本地重复该问题,那么您应该能够看到问题或至少获得一个 PCAP - 然后您可以在此处发布它,并且应该可以有人给您一个更好的答案。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2015-07-15
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多