C# 用C编写的从chrome扩展到本机主机的本机消息传递#

C# 用C编写的从chrome扩展到本机主机的本机消息传递#,c#,google-chrome-extension,chrome-native-messaging,C#,Google Chrome Extension,Chrome Native Messaging,我正试图通过本机消息接收来自chrome扩展的消息。popup.html控制台指示消息正在发送,但我的主机由于某种原因没有接收到消息。我可以看到正在任务管理器中启动主机native.exe,但主机未接收发送的数据 popup.js document.addEventListener('DOMContentLoaded', function() { var downloadButton= document.getElementById('Button'); downloadButt

我正试图通过本机消息接收来自chrome扩展的消息。
popup.html
控制台指示消息正在发送,但我的主机由于某种原因没有接收到消息。我可以看到正在任务管理器中启动主机
native.exe
,但主机未接收发送的数据

popup.js

document.addEventListener('DOMContentLoaded', function() {
    var downloadButton= document.getElementById('Button');
    downloadButton.addEventListener('click', function () {
        chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
            chrome.tabs.executeScript(tabs[0].id, {file: "myScript.js"}, function (data) {
                sendNativeMessage(data[0]);
            });
        });
    });
});

function sendNativeMessage(msg) {
    var hostName = "com.google.example";

    console.log("Connecting to host: " + hostName);
    port = chrome.runtime.connectNative(hostName);

    message = {"text": msg};
    port.postMessage(message);

    console.log("Sent message: " + JSON.stringify(message));
}
现在,当我从Chrome扩展发送消息时,我可以看到
popup.html
的控制台日志,消息是正确的

我试着用一些有人声称工作过的代码来测试一切,从。具体来说,我有

native.exe

    static void Main(string[] args) {
        while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
        {
            Console.WriteLine(OpenStandardStreamIn());
        }
    }

    private static string OpenStandardStreamIn()
    {
        //Read first four bytes for length information
        System.IO.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;
    }
这也不管用。我不知道我做错了什么,也不知道为什么当我从分机发送消息时,我的主机
.exe
停止启动


编辑#2

好消息

我删除了我的注册表项,然后再次将其添加到本地计算机注册表(即HKLM)中。同样,我使用来自的代码从主机发送和接收。现在,当我查看扩展日志时,我可以看到来自主机的正确响应。我使用的是“无端口”方法,只需调用
chrome.runtime.sendNativeMessage(…)
,这对我来说是很好的。不幸的是,我没有收到来自主机扩展的消息。此外,my
host.exe
永远不会退出,即使在收到消息之后也是如此。我不知道为什么,但如果有人能帮忙,我将不胜感激

在我的主机中,我试图将传入消息写入文件,只是为了测试我是否收到消息:

System.IO.File.WriteAllText(@"path_to_file.txt", OpenStandardStreamIn());
注意:我正在从扩展传递到主机的消息大约为250KB(它因消息而异)

问题是:

  • 文件中未写入任何内容。它完全是空白的。
  • 创建文件需要很长时间(~4秒)
  • 我的
    host.exe
    实例从未终止。它仍在运行(我可以在任务管理器中查看)

  • 为了让我的分机正常工作,我遵循了以下步骤:

  • 我删除了我的注册表项,然后将其重新添加到我的HKLM注册表中
  • 虽然没有必要,
    host.exe实际上只需要从扩展接收一条消息,所以我将以前的“开放端口”消息更改为“无端口”消息,即

    chrome.runtime.sendNativeMessage(…)

  • 重要的是,当我将发送的文本从
    {“text”:data[0]}
    更改为一个简单的字符串
    {“text”:“Hello from Chrome!”}
    时,一切都开始顺利进行
    data[0]
    是一个大约250KB的字符串,根据上下文,它应该是一个可以接受的消息大小。但是,我为这个问题创建了一个解决方案,因为我能够解决我遇到的一般消息发送和接收问题


  • 你试过记录chrome.runtime.lastError.message吗?@Xan是的。我发现
    未找到指定的本机消息传递主机
    错误。我在HKCU注册表中创建了我的注册表项,并将其指向我的
    主机清单.json
    ,我将其存储在与我的
    主机.exe
    相同的目录中,这只是为了更好地测量。另外,昨天,当我从我的扩展发送消息时,
    host.exe
    的一个实例出现在taskmgr中,所以我不确定它为什么会坏。@Xan我修复了
    主机未找到
    错误,但现在我仍然无法实际接收来自Chrome扩展的消息。我还试图弄清楚为什么创建文件后我的
    host.exe
    没有终止。见编辑#2。
    System.IO.File.WriteAllText(@"path_to_file.txt", OpenStandardStreamIn());