c# - 原生消息 Chrome

标签 c# javascript google-chrome google-chrome-extension chrome-native-messaging

我正在尝试在我的 chrome 扩展和我的 c# 应用程序之间获取 native 消息传递。 javascript 工作正常,但我收到此错误:

Error when communicating with the native messaging host.

正如我在任务管理器中看到的那样,应用程序确实随扩展一起启动。 这是我的 C# 代码。

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 < length;i++ )    
    {
        input += (char)stdin.ReadByte();
    }

    return input;  
}

private static void OpenStandardStreamOut(string stringData)
{
    //// We need to send the 4 btyes of length information
    string msgdata = "{\"text\":\"" + stringData + "\"}";
    int DataLength = stringData.Length;
    Stream stdout = Console.OpenStandardOutput();
    stdout.WriteByte((byte)((DataLength >> 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);
}

主要功能:

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代码:

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;
}

主机 list :

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

最佳答案

是的,那是因为您发送了错误的数据长度。在 OpenStandardStreamOut 函数中将 stringData.Length 更改为 msgdata.Length

关于c# - 原生消息 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219144/

相关文章:

java - 未捕获的类型错误 : undefined is not a function Chrome not working

javascript - 如何在页面重新加载时清除 JavaScript 变量?

c# - 为什么我的 UDP 数据包发送到我自己的公共(public) IP 时没有收到?

c# - 输入不是有效的 Base64 字符串,因为它包含非 base 64 字符?

javascript - jquery源码简单, "this"关键字容易混淆

javascript - 如何使按钮消失并在 'x' 秒后重新出现?

css - 透明导航栏不适用于 Chrome

c# - 将页面从 pdf 文件复制到新文档

C# Web API - 内部服务器错误 500

javascript - 传单折线 SmoothFactor 范围