关闭单节点服务器连接将全部关闭

Closing single node server connection closes them all

我几乎不问任何关于 Stack Overflow 的问题,但这个问题超出了我的范围。我想我缺少一些基本的东西,因为我是 Node 服务器的新手。

我们的应用程序非常基础。服务器应该接收少量文本行(数据),合并并解析它们,一旦连接关闭(数据发送结束),它就会将数据发送到 api.

var net = require('net');
var fs = require('fs');
const axios = require('axios')

const server = new net.Server();
server.listen(PORT, IP);

server.on("connection", client => {
    client.write("Hello\n");
    console.log('connected');
    
    let received = "";
    client.on("data", data => {
       received += data
       console.log("Partial data is: " + data);
    });

    client.on("close", () => {
        received = received.toString('utf8');
        fs.appendFile('log.txt', received, function (err) {});

        received = received.replace(/(?:\r\n|\r|\n)/g, "||");
        axios.post(APIADDRESS, {data: received});
        console.log('Full data is: '+ {data: received});
    });
});

要发送数据,我只是 运行 一个 netcatnc 使用 netcat ipaddress port,这不是问题。连接正常,已收到状态消息。

问题是 - 一旦我从两个不同的 SSh 服务器打开两个或多个连接,就会发生一些奇怪的事情。我可以一行接一行地发送就好了。服务器报告“部分数据”调试没有问题,对于他们两个。 但是,一旦我关闭其中一个连接 (ctrl+c),它们都会关闭。 最后,只收到来自手动关闭连接的数据。另一个来自单独的 ssh 服务器上的单独 nc 似乎永远不会到达 client.on("close") 部分。就这样无缘无故地被终止了。

有什么想法吗?我什至不知道从哪里开始。

//编辑 刚刚从我的电脑和一些使用单独的 SSH 服务器的 ssh 移动应用程序对其进行了测试。一旦在任何设备上发送 ctrl+c,它就会关闭所有客户端的连接。

//忘了说我是 运行 pm2 来保持服务器正常运行。一旦我手动打开脚本,忽略 pm2 - 它工作正常。诡异的。这是因为 PM2.

我猜您已将 Putty 配置为“尽可能共享 SSH 连接”。根据某些文档,这样做时:

When this mode is in use, the first PuTTY that connected to a given server becomes the ‘upstream’, which means that it is the one managing the real SSH connection. All subsequent PuTTYs which reuse the connection are referred to as ‘downstreams’: they do not connect to the real server at all, but instead connect to the upstream PuTTY via local inter-process communication methods.

因此,如果您 Ctrl+C 管理实际共享连接的 PuTTY 会话,它们都会失去连接。

您大概可以在客户端或服务器端禁用此共享连接功能,因为两者都必须启用才能进行共享。

给以后来这里的任何人。

如果您使用 pm2 并启用 --watch 并且文本日志文件与您的主服务器脚本位于同一文件夹中......这就是它删除的原因单个客户端断开连接后的连接。它只是检测到日志已更改。

我不是在打脸,那一点都不好笑。