节点 `net` 模块 IPC 服务器间歇性
Node `net` module IPC server intermitent
根据 net
和 child_process
模块的官方节点文档,我将其存档:生成子节点并通过 net
模块连接的服务器。但是连接是断断续续的。代码不言自明,但我在代码注释中添加了详细信息:
// server.js
const childProcess = require('child_process').fork('child.js');
const server = require('net').createServer((socket) => {
console.log('got socket connection'); // this callback is intermitent
socket.on('data', (stream) => {
console.log(stream.toString());
})
});
server.on('connection', () => {
console.log('someone connected to server'); // this is running only if the code above runs (but its intermitent)
});
server.on('listening', () => {
console.log('server is listening'); // this is the first log to execute
childProcess.send('server', server); // send the server connection to forked child
});
server.listen(null, function () {
console.log('server listen callback'); // this is the second log to execute
});
// child.js
console.log('forked'); // this is the third log to execute
const net = require('net');
process.on('message', (m, server) => {
if (m === 'server') {
const socket = net.connect(server.address());
socket.on('ready', () => {
console.log('child is ready'); // this is the fourth log to execute
socket.write('child first message'); // this is always running
})
}
});
执行node server
时预期的日志是:
server is listening
server listen callback
forked
child is ready
got socket connection
someone connected to server
child first message
但是由于套接字回调(在 createServer
)是间歇性的,我们有 50% 的时间得到这个:
server is listening
server listen callback
forked
child is ready
不知道该怎么办,我已经尽力了...我做错了什么?
刚刚发现问题所在...当我阅读文档时我误解了 net
服务器被发送到子进程以共享“连接”以将处理分成多个一个过程,而我试图存档的只是与分叉子的双向通信。如果有人遇到与我相同的问题,我会在这里回答。这是最终代码:
// server.js
const childProcess = require('child_process').fork('child.js');
const server = require('net').createServer((socket) => {
console.log('got socket connection');
socket.on('data', (stream) => {
console.log(stream.toString());
})
});
server.on('connection', () => {
console.log('someone connected to server');
});
server.listen(null, function () {
childProcess.send(server.address());
});
// child.js
console.log('forked');
const net = require('net');
process.on('message', (message) => {
if (message.port) {
const socket = net.connect(message);
socket.on('ready', () => {
console.log('child is ready');
socket.write('child first message');
})
}
});
根据 net
和 child_process
模块的官方节点文档,我将其存档:生成子节点并通过 net
模块连接的服务器。但是连接是断断续续的。代码不言自明,但我在代码注释中添加了详细信息:
// server.js
const childProcess = require('child_process').fork('child.js');
const server = require('net').createServer((socket) => {
console.log('got socket connection'); // this callback is intermitent
socket.on('data', (stream) => {
console.log(stream.toString());
})
});
server.on('connection', () => {
console.log('someone connected to server'); // this is running only if the code above runs (but its intermitent)
});
server.on('listening', () => {
console.log('server is listening'); // this is the first log to execute
childProcess.send('server', server); // send the server connection to forked child
});
server.listen(null, function () {
console.log('server listen callback'); // this is the second log to execute
});
// child.js
console.log('forked'); // this is the third log to execute
const net = require('net');
process.on('message', (m, server) => {
if (m === 'server') {
const socket = net.connect(server.address());
socket.on('ready', () => {
console.log('child is ready'); // this is the fourth log to execute
socket.write('child first message'); // this is always running
})
}
});
执行node server
时预期的日志是:
server is listening
server listen callback
forked
child is ready
got socket connection
someone connected to server
child first message
但是由于套接字回调(在 createServer
)是间歇性的,我们有 50% 的时间得到这个:
server is listening
server listen callback
forked
child is ready
不知道该怎么办,我已经尽力了...我做错了什么?
刚刚发现问题所在...当我阅读文档时我误解了 net
服务器被发送到子进程以共享“连接”以将处理分成多个一个过程,而我试图存档的只是与分叉子的双向通信。如果有人遇到与我相同的问题,我会在这里回答。这是最终代码:
// server.js
const childProcess = require('child_process').fork('child.js');
const server = require('net').createServer((socket) => {
console.log('got socket connection');
socket.on('data', (stream) => {
console.log(stream.toString());
})
});
server.on('connection', () => {
console.log('someone connected to server');
});
server.listen(null, function () {
childProcess.send(server.address());
});
// child.js
console.log('forked');
const net = require('net');
process.on('message', (message) => {
if (message.port) {
const socket = net.connect(message);
socket.on('ready', () => {
console.log('child is ready');
socket.write('child first message');
})
}
});