问题描述
我正在编写一个AIR应用程序,该应用程序将启动C#控制台应用程序,并且他们需要进行交流。我想为此使用标准输入/标准输出,但是似乎无法正常使用。
I'm writing an AIR app that launches a C# console application and they need to communicate. I'd like to use standard input/standard output for this, however I can't seem to get it to work.
当C#应用从标准输入中获取输入时,应该通过标准输出将其发送回去,如果收到退出,则退出。我可以从命令行进行测试,并且可以正常工作。从AIR发送字符串时,C#应用程序没有响应。
When the C# app gets input from standard input it's supposed to send it back via standard output, and if it receives "exit" then it quits. I can test this from the command line and it works correctly. When I send a string from AIR, I get no response from the C# app.
启动C#应用程序时我正在发送一个参数,但确实得到了响应,所以我的AIR应用程序至少能够接收来自标准的消息输出,只是标准输入不起作用。当我通过standardInput从AIR发送消息时,我收到一个进度事件,发送键码时为bytesLoaded = 3,发送退出命令时为bytesLoaded = 5。
I'm sending an argument when I launch the C# app, and I do get a response from that, so my AIR app is at least able to receive messages from standard output, it's just standard input that is not working. When I send a message from AIR via standardInput I get a progress event with bytesLoaded = 3 when I send the keycode, and bytesLoaded = 5 when I send the "exit" command.
这是C#代码:
static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine(args[0]);
}
while (true)
{
string incoming = Console.ReadLine();
string outgoing = "received: " + incoming;
Console.WriteLine(outgoing);
if (incoming == "exit")
return;
}
}
这是AS3代码:
private function init(e:Event=null):void {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
NativeApplication.nativeApplication.addEventListener(Event.EXITING, onAppClose);
var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = File.applicationDirectory.resolvePath("test.exe");
info.executable = file;
process = new NativeProcess();
info.arguments.push("native process started");
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, onInputProgress);
process.addEventListener(Event.STANDARD_OUTPUT_CLOSE, onOutputClose);
process.addEventListener(Event.STANDARD_ERROR_CLOSE, onErrorClose);
process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
process.addEventListener(IOErrorEvent.STANDARD_INPUT_IO_ERROR, onIOError);
process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
process.start(info);
}
private function onKeyUp(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.ESCAPE)
process.standardInput.writeUTFBytes("exit\n");
else {
var msg:String = e.keyCode + "\n";
process.standardInput.writeUTFBytes(msg);
}
}
private function onOutputData(e:ProgressEvent):void {
var data:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
trace("Got: ", data);
}
推荐答案
我遇到了这个问题几个月前,但我从未解决过它,因为我只是使用了命令行参数。我只是回到了它,因为我很想知道发生了什么事情。
I encountered this issue a few months back but I never resolved it as I just used command line args instead. I have just returned to it though as I am keen to find out know what's going on.
我现在发现以.NET 3.5或更早版本为目标可以使其按预期工作为了我。切换回v4.0,我在stdin上一无所获。我不太清楚问题出在哪里,但是如果您可以使用v3.5,那么这可能是您的解决方案。
I have now found that targeting .NET 3.5 or earlier makes it work as expected for me. Switch back to to v4.0 and I get nothing on stdin. I'm not exactly sure where the issue lies, but if you can get by with v3.5 then it might be a solution for you.
这篇关于AIR NativeProcess standardInput未连接到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!