简体   繁体   中英

Launching an exe from node.js, and send data between them

Ok, so I'm creating a webpage using socket.io in node.js. This works great, and data is going to each device as it should. Now i want to expand, and use node.js to control my pc.

I've read this: Child Process and gotten to this code wich runs an executable, and prints its output in the console.

var spawn = require('child_process').spawn;

var ahk = spawn('outsideNode.exe');
ahk.stdout.on('data', function(data){
    console.log(data.toString());
});

Here is the code, or script for outsideNode.exe that gets launched. I'm using AutoHotKey for this.

Loop,10
{
    FileAppend,External program spawned by Node.js says hi!,*
    Sleep,1000
}
ExitApp

This works one way. Node.js captures AHK's output. Now, the problem i'm having is, how do i SEND data from node.js? And how do i recieve it in AHK? I do belive i need to read about stdio. But i'm new to the std's.

My current solution is using Node.js to write a .txt file to disk with the command i want to give, and a seperate .exe file that reads the .txt and executes the command and exits. This works, but it is slow-ish, and just feels wrong to do it this way.
Also, I want it to be asynchronous, or synchronous? The one where i can do other stuff with node.js while the executable does its thing. Now i have to wait to be sure the file can be overwritten.

Any input on how to this is greatly appreciated.

There is the option to pass arguments to the spawned app. Maybe that's already what you are looking for. How to handle the arguments in your exe is another question related to some other programming languages. In node, child processes run asynchronous and non-blocking, that's the one you are looking for. However, if you want to communicate between the programs, you can write to the stdin of the childprocess like so:

child.stdin.write("here you go");

Your exe must be able to wait for stdin in order to process the incoming data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM