【发布时间】:2014-12-18 20:01:51
【问题描述】:
我正在尝试从 node.js 与 windows EXE 进行通信。我对 Node.js 还很陌生
使用此示例作为起点,但我无法让它工作。
http://juristr.com/blog/2014/03/integrating-node-with-csharp/
看起来可执行文件没有启动,因为当我运行它时 .connected 为假。 我看不到可执行文件的启动位置/方式。
EXE 代码(从命令行运行它工作正常,返回输入的任何内容)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryExe
{
class Program
{
static void Main(string[] args)
{
string line;
do
{
line = Console.ReadLine();
try
{
Console.WriteLine("Recieved - " + line);
if (line == "quit")
break;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} while (line != null);
}
}
}
Node.js 代码(好像没有启动exe,connected = false)
console.log('Hello world');
var spawn = require('child_process').spawn;
var posProc = spawn('C:/path/LibraryExe.exe', []);
posProc.stdout.once('data', function (data) {
// write it back on the response object
console.log('stdOut');
writeToResponse('we got back ' + data);
});
posProc.on('exit', function (code) {
console.log('Closed');
writeToResponse('Library closed');
});
posProc.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
if (posProc.connected)
console.log('connected');
else
console.log('not connected');
posProc.stdin.setEncoding = 'utf-8';
posProc.stdin.write('Hello');
posProc.stdin.write('quit');
console.log('ending');
【问题讨论】:
标签: node.js stdin child-process