Heyyo,I learned child processes by putting together the following script, but I'm a little confused about some of the results pertaining to which events fire and which don't.In the below code, you will notice both main.js and child.js have sections at the bottom, "code ghetto -- garbage that does nothing" ... my question is, can you explain why each callback isn't being triggered? Because, aside from the error handlers, I would have expected all of these event handlers to fire at their appropriate times; but when I quit the client, all the parent-side handlers fire, but none of the child-side handlers fire. That seems counter-intuitive to me...Is it just that the child events are only fired if the corresponding action is taken in the child process? Even if so, why does child.close never fire (code ghetto of main.js)?Here's the code:main.jsconst serverPort = 81; const server = require('net').createServer(); server.listen(serverPort, () => { console.log(`server is listening on port ${serverPort}`); }); server.on('connection', connListener); server.on('error', (e) => { console.log('server error'); throw e;} ); function connListener(socket) { // launch client: fork child process const child = require('child_process').fork('child.js'); // this is the server on-connect handler; say hello to the new connection... socket.write('hello\r\n'); socket.pipe(socket); // setup child process event handlers child.on('disconnect', () => { console.log('parent: child disconnected IPC channel'); }); child.on('error', (error) => { console.log('parent: child error reported by parent'); throw error; }); child.on('exit', () => { console.log('parent: child process reports el-quitto'); if (!child.send) { console.log('parent: it is dead, james'); } }); child.on('message', () => { console.log('parent: receiving message from child'); }); // setup socket event handlers socket.on('data', (data) => { child.send(data.toString()); }); socket.on('end', () => { console.log('parent: socket end; killing child and socket'); child.disconnect(); socket.end(); socket.destroy() child.kill(); }); socket.on('close', () => { console.log('parent: socket = X_x'); }); socket.on('error', (error) => { console.log('parent: socket error'); throw error; }); /* code ghetto -- garbage that does nothing */ child.on('close', () => { // this never fires console.log('parent: child reports x_X'); }); } child.js:process.on('message', (data) => { if (data == "\r\n") console.log('child: return detected'); else console.log('child: received data: ' + data); }); console.log('child: process has launched'); /* code ghetto -- garbage that does nothing */ process.on('close', () => { console.log('child: child = x_X'); }); process.on('disconnect', () => { console.log('child: disconnecting IPC'); }); process.on('error', (error) => { console.log('child: child error'); throw error; }) process.on('exit', () => { console.log('child: child process el-quitto'); }); EDIT: outputrunning the above and telnetting to localhost port 81, hitting some keys, and then closing the telnet client, produces:$ node . server is listening on port 81 child: process has launched child: received data: ���� ����'������ child: received data: ���� ����'�� child: received data: ��$ child: received data: ��$ child: received data: a child: received data: s child: received data: d child: received data: f child: received data: a child: received data: s child: received data: d child: received data: f child: received data: s child: received data: a child: received data: d child: received data: f parent: socket end; killing child and socket parent: child disconnected IPC channel parent: socket = X_x parent: child process reports el-quitto Back to the question, I'm trying to understand why the various child events don't fire.EDIT2: did I f* up some callback signatures?
Submitted November 15, 2018 at 02:07AM by poor-toy-soul-doll
No comments:
Post a Comment