我有一个正在运行的Express实例,需要在一个单独的对等非Express节点进程中触发事件。该事件不是经常发生的事件,因此在客户端(快速过程)发送其消息之后关闭连接似乎是明智的。
我已经浏览了https://www.npmjs.com/package/node-ipc上的文档,并且关闭连接的唯一方法似乎是调用ipc.disconnect('serverId')。我已经在代码的几个不同点尝试过此方法,但是客户端在断开连接后最终尝试重新连接。我显然做错了。
这是客户端代码。
router.get("/test", async (req, res) => {
ipc.config.id = 'socialRequest';
ipc.config.retry = 1500;
ipc.config.maxRetries = 2;
ipc.config.silent = false;
await ipc.connectTo(
'socialQuery',
() => {
ipc.of.socialQuery.on(
'connect',
async () => {
await ipc.of.socialQuery.emit(
'queryAllOnce',
{
id : ipc.config.id,
message : 'Goodbye '
}
);
ipc.disconnect('socialQuery');
}
);
ipc.of.socialQuery.on(
'disconnect',
function(){
ipc.log('disconnected from socialQuery');
}
);
ipc.of.socialQuery.destroy;
}
);
return res.json({
"msg": "hi"
});
});
和服务器代码
// IPC server
var ipc=require('node-ipc');
ipc.config.id = 'socialQuery';
ipc.config.retry= 1500;
ipc.serve(() => {
ipc.server.on(
'queryAllOnce',
function(data, socket){
console.log(`got a message : ${JSON.stringify(data)} ${JSON.stringify(socket.__proto__)}`);
ipc.log(`got a message : ${data} ${socket}`);
}
);
ipc.server.on(
'socket.disconnected',
function(socket, destroyedSocketID) {
ipc.log(`client ${destroyedSocketID} disconnected`);
}
);
}
);
ipc.server.start();