I have a simple program that reads from child output and pass it through 
websocket

However, when the child output very long string, nodejs quits immediately, 
no errors, sometimes I can see the 'clean up' message, sometimes I couldn't.

Anyone know why?

var peers = [];

child = require('child_process').execFile('bash', [ 
    'xxxx.sh'
], { 
    detached: true, 
    stdio: [ 'ignore', 1, 2 ], 
}); 


child.unref(); 
child.stdout.on('data', function(data) {
  data = data.toString();
  peers.forEach(function(peer) {
    peer.sendText(data);
  })
  console.log('[STDOUT]', data);
});
child.stderr.on('data', function(data) {
  data = data.toString();
  console.log('[STDERR]', data);
});
child.on('error', function(err) {
  console.log('error in child process', err);
  process.exit(1);
});
child.on('close', function(code) {
  process.exit(code);
});


function cleanup() {
    console.log('Cleaning up');
    child.kill('SIGINT');
}


process.on('exit',   cleanup);
process.on('SIGINT', cleanup);


var ws = require("nodejs-websocket")
var server = ws.createServer(function(conn) {
  console.log("New connection")


  peers.push(conn);
  conn.on("text", function(str) {
    console.log('Received', str);    
  })
  conn.on("close", function(code, reason) {
    console.log("Connection closed");
    for (var i = 0; i < peers.length; i ++) {
      if (peers[i] === conn) {
        console.log('Removed connection #', i);
        peers.splice(i, 1);
        break;
      }
    }
  })
}).listen(8300)


-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/7852aa95-b27e-494b-83da-07d36bff7e95%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to