Node.js PM2与NodeJS dgram
我正在尝试使用pm2运行NodeJS应用程序(udp_recv)。 应用程序在引导时通过pm2启动和pm2保存启动。 但是,dgram给了我一个错误,说-Node.js PM2与NodeJS dgram,node.js,udp,pm2,dgrams,Node.js,Udp,Pm2,Dgrams,我正在尝试使用pm2运行NodeJS应用程序(udp_recv)。 应用程序在引导时通过pm2启动和pm2保存启动。 但是,dgram给了我一个错误,说- server error: 1|udp_recv | Error: bind EADDRNOTAVAIL 192.168.0.9:7001 1|udp_recv | at state.handle.lookup (dgram.js:242:18) 1|udp_recv | at process._tickCallback (i
server error:
1|udp_recv | Error: bind EADDRNOTAVAIL 192.168.0.9:7001
1|udp_recv | at state.handle.lookup (dgram.js:242:18)
1|udp_recv | at process._tickCallback (internal/process/next_tick.js:63:19)
1|udp_recv | at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
1|udp_recv | at startup (internal/bootstrap/node.js:283:19)
1|udp_recv | at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
但一旦我停止所有应用程序(pm2停止全部)并重新运行(pm2启动全部)
然后没有错误,应用程序正常运行,没有任何错误
我的IP是192.168.0.9,在预定义端口上侦听UDP流。通过以太网连接的计算机从IP 192.168.0.6发送UDP流
有人能告诉我如何解决这个错误吗?
事实证明,通过dgram重新启动后立即绑定可能会导致错误。
因为“EADDRNOTAVAIL”表示地址不可用
解决方案将socket.bind上的setTimeout设置为60秒左右,它就可以正常工作了
setTimeout(() => {
server.bind({
address: "127.0.0.1",
port: process.env.SERVER_PORT,
exclusive : true
})
}, 60000)
另外,另一种解决方案是尝试在服务器错误“EADDRNOTAVAIL”时重新连接/绑定
//If server(app.js) gets any error
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
//Server is closed
//server.close();
//add an "if" block to check if the error is of type "EADDRNOTAVAIL"
server.bind({
address: "127.0.0.1",
port: process.env.SERVER_PORT,
exclusive : true
})
});