Native Messaging Chrome

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I am trying to get Native Messaging between my chrome extension and my c# application. The javascript works fine, but I am getting this error:

Error when communicating with the native messaging host.

The application does get launched along with the extension, as I saw from Task Manager. Here is my c# code.

private static string OpenStandardStreamIn() {     //// We need to read first 4 bytes for length information     Stream stdin = Console.OpenStandardInput();     int length = 0;     byte[] bytes = new byte[4];     stdin.Read(bytes, 0, 4);     length = System.BitConverter.ToInt32(bytes, 0);      string input = "";     for (int i = 0; i > 0) & 0xFF));     stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));     stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));     stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));     //Available total length : 4,294,967,295 ( FF FF FF FF )     Console.Write(msgdata); } 

And the main function:

static void Main(string[] args) {     string message = "test message from native app.";     OpenStandardStreamOut(message);      while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")     {         OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn());         OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn());       } } 

JS Code:

var host_name = "com.example.native"; var port = null;  connectToNative(); function connectToNative() {     console.log('Connecting to native host: ' + host_name);     port = chrome.runtime.connectNative(host_name);     port.onMessage.addListener(onNativeMessage);     port.onDisconnect.addListener(onDisconnected);     sendNativeMessage("test"); }  function sendNativeMessage(msg) {     message = {"text" : msg};     console.log('Sending message to native app: ' + JSON.stringify(message));     port.postMessage(message);     console.log('Sent message to native app: ' + msg); }  function onNativeMessage(message) {     console.log('recieved message from native app: ' + JSON.stringify(msg)); }  function onDisconnected() {     console.log(chrome.runtime.lastError);     console.log('disconnected from native app.');     port = null; } 

Host Manifest:

{   "name": "com.example.native",   "description": "Native support for Chrome Extension",   "path": "NativeApp.exe",   "type": "stdio",   "allowed_origins": [     "chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/"   ] }   

回答1:

Yeah that's because you send a wrong data length. Change stringData.Length to msgdata.Length in your OpenStandardStreamOut function.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!