Saturday 11 March 2017

Node.js (nwjs) child_process.spawn returning continuous output

I have a command line application called interface. If I open it up from the terminal, it will sit and wait for input. I type in a json string, and it returns a json string. For example:

JZ:MacOS jcz$ ./interface
{"type":"test"}
{"msg": "Invalid command: test", "type": "error"}

I am trying to launch this process from a nwjs application, so I can send and receive data. What is happening is that the process seems to think I am continuously sending commands to the process when I haven't sent anything at all.

var spawn = require('child_process').spawn;
var py = spawn('./py/dist/interface.app/Contents/MacOS/interface');

py.stdout.on('data', function(data) {
    console.log(data.toString());
});

Without having sent anything at all, if I do py.stdin.end() I get an unending stream:

{"msg": "Could not parse JSON message", "type": "error"}
{"msg": "Could not parse JSON message", "type": "error"}
{"msg": "Could not parse JSON message", "type": "error"}
{"msg": "Could not parse JSON message", "type": "error"}
...

Which is the error message I have programmed in when invalid JSON input is sent.

I would like to be able to send and receive messages to the process using py.stdin.write(string) and have the output appear in the console.



via Jeff

No comments:

Post a Comment