【问题标题】:Chrome native messageChrome 原生消息
【发布时间】:2014-12-11 12:53:51
【问题描述】:

我正在开发“Chrome Native Messaging”的示例应用程序。我按照网站上提到的步骤完成了所有设置。我也可以运行应用程序,但是我没有从本机应用程序收到响应消息。当我第一次开始扩展时,我会收到响应消息。

Downloaded sample from here

当我从浏览器本机应用程序发送消息没有响应时,请检查下图

C#代码如下

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

    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 = msgdata.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);
    }

manifest.json 如下

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

有些地方我觉得我们没有收到来自本机主机的响应,因为我已经在浏览器中的以下代码中添加了调试点,但没有被命中

function onNativeMessage(message) {

appendMessage("收到的消息:" + JSON.stringify(message) + ""); }

我错过了什么吗?

【问题讨论】:

  • 我得到:“Uncaught TypeError: chrome.runtime.connectNative is not a function”

标签: c# google-chrome chrome-native-messaging


【解决方案1】:

我遇到了同样的问题。确保您发送的消息是有效的 JSON。在我的例子中,主机收到的值是"some value" 带双引号。当它被连接以生成 msgdata 变量时,它正在创建无效的 JSON。

【讨论】:

  • @user34660,有可能,或者他在我评论后删除了他的评论。无论哪种方式,现在都没有多大意义。我会删除我的评论。
猜你喜欢
  • 2014-08-04
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多