gpt4 book ai didi

node.js - 如何从 Node js 中的 'net' 模块捕获 ECONNRESET 错误?并防止整个程序被终止?

转载 作者:行者123 更新时间:2023-12-03 08:38:59 25 4
gpt4 key购买 nike

如果我尝试 NodeJS 文档中的非常基本的代码

//CLIENT.js
const net = require('net');
const client = net.createConnection({ port: 8124 }, () => {
// 'connect' listener.
console.log('connected to server!');
client.write('I am client');
});
client.on('data', (data) => {
console.log(data)
client.write('I am client');
//client.end(); I don't want to close the connection
});
client.on('end', () => {
console.log('disconnected from server');
});

//SERVER.js
const net = require('net');
const server = net.createServer((c) => {
// 'connection' listener.
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.on('data', (data) => {
console.log(data)
c.write("I am server");
})
c.pipe(c);
});
server.on('error', (err) => {
console.log("Something went worng"); //I hope this will be triggered in cases of error
});
server.listen(8124, () => {
console.log('server bound');
});

现在的问题: -
  • 我启动 SERVER.js node SERVER.js
  • 然后我启动 CLIENT.js node CLIENT.js在不同的 CMD 选项卡上。
  • 到目前为止一切顺利......'我是服务器我是客户............'

  • 备注 我正在使用 Windows 10
  • 现在,我只需按 ctrl + C 即可关闭客户端程序。 ,然后服务器端程序被终止并抛出“read ECONNRESET”

  • 完整的错误日志
    events.js:292
    throw er; // Unhandled 'error' event

    Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
    Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    errno: 'ECONNRESET',
    code: 'ECONNRESET',
    syscall: 'read'
    }
    我理解为什么理论上会导致这个错误,所以我尝试以不同的方式解决它,因为程序没有触发 server.on('error')事件,它只是终止了整个服务器程序。
    所以我尝试的是
    //SERVER.js

    const net = require('net');
    const server = net.createServer((c) => {
    // 'connection' listener.
    console.log('client connected');
    try {
    c.on('end', () => {
    console.log('client disconnected');
    });
    c.on('data', (data) => {
    console.log(data)
    c.write("I am server");
    })
    c.pipe(c);
    } catch(e){
    //hoping of catching the error
    console.log("Here is your error", e)
    // !!! But no error is caught here and the program still gets completely terminated
    }
    });
    server.on('error', (err) => {
    console.log("Something went worng"); //I hope this will be triggered in cases of error
    });
    server.listen(8124, () => {
    console.log('server bound');
    });

    那么我应该如何处理“读取 ECONNRESET”错误呢?

    最佳答案

    如果您环绕 try-catch 整个代码并在 catch 中再次调用 main/starting 函数,它将再次启动,但它将进入 try-catch 的无限循环,直到您获得 ECONNRESET 的服务不再启动,
    确保您的应用程序所依赖的服务是 99.99999% 的时间,并让您的应用程序失败并重新启动。 (例如,使用 PM2 类型的实用程序)

    关于node.js - 如何从 Node js 中的 'net' 模块捕获 ECONNRESET 错误?并防止整个程序被终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63897132/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com