本机消息传递Chromejavascript工作正常,但我收到此错误:与本机消息传递主机通信时出错。正如我从任务管理器中看到的那样,应用程序确实以扩展名开始。这是我的C#代码。privatestaticstringOpenStandardStreamIn(){////我们需要读取长度信息的前4个字节Streamstdin=Console.OpenStandardInput();整数长度=0;byte[]bytes=newbyte[4];stdin.Read(字节,0,4);length=System.BitConverter.ToInt32(bytes,0);字符串输入=“”;对于(inti=0;i>0)&0xFF));stdout.WriteByte((byte)((DataLength>>8)&0xFF));stdout.WriteByte((byte)((DataLength>>16)&0xFF));stdout.WriteByte((byte)((DataLength>>24)&0xFF));//可用总长度:4,294,967,295(FFFFFFFF)Console.Write(msgdata);}主要功能:staticvoidMain(string[]args){stringmessage="testmessagefromnativeapp.";OpenStandardStreamOut(消息);while(OpenStandardStreamIn()!=null||OpenStandardStreamIn()!=""){OpenStandardStreamOut("接收到本机应用程序:"+OpenStandardStreamIn());OpenStandardStreamOut("收到:"+OpenStandardStreamIn());}}JS代码:varhost_name="com.example.native";变量端口=空;connectToNative();functionconnectToNative(){console.log('连接到本机主机:'+host_name);port=chrome.runtime.connectNative(host_name);port.onMessage.addListener(onNativeMessage);port.onDisconnect.addListener(onDisconnected);sendNativeMessage("测试");}functionsendNativeMessage(msg){message={"text":msg};console.log('正在向本机应用程序发送消息:'+JSON.stringify(message));port.postMessage(消息);console.log('发送消息给原生应用:'+msg);}functiononNativeMessage(message){console.log('从本机应用程序收到消息:'+JSON.stringify(msg));}functiononDisconnected(){console.log(chrome.runtime.lastError);console.log('与本机应用断开连接。');端口=空;}主机清单:{"name":"com.example.native","description":"NativesupportforChromeExtension","path":"NativeApp.exe","type":"stdio","allowed_origins":["chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/"]}是的,因为你发送的数据长度错误在你的OpenStandardStreamOut函数中将msgdata.Length改为msgdata.Length以上就是C#学习教程的全部内容:原生消息Chrome分享,如果对大家有用以及需要了解更多C#学习教程,希望大家多多关注~本文整理自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除,如需转载请注明出处:
