【问题标题】:C# native host with Chrome Native Messaging带有 Chrome 原生消息的 C# 原生主机
【发布时间】:2015-09-02 01:07:04
【问题描述】:

我今天花了几个小时研究如何让 Chrome 原生消息传递与 C# 原生主机一起工作。从概念上讲,这很简单,但我在其他问题的帮助下(部分)解决了一些问题:

Native Messaging Chrome
Native messaging from chrome extension to native host written in C#
Very Slow to pass "large" amount of data from Chrome Extension to Host (written in C#)

我的解决方案贴在下面。

【问题讨论】:

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


    【解决方案1】:

    假设清单设置正确,以下是使用“端口”方法与 C# 主机通信的完整示例:

    using System;
    using System.IO;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    namespace NativeMessagingHost
    {
       class Program
       {
          public static void Main(string[] args)
          {
             JObject data;
             while ((data = Read()) != null)
             {
                var processed = ProcessMessage(data);
                Write(processed);
                if (processed == "exit")
                {
                   return;
                }
             }
          }
    
          public static string ProcessMessage(JObject data)
          {
             var message = data["text"].Value<string>();
             switch (message)
             {
                case "test":
                   return "testing!";
                case "exit":
                   return "exit";
                default:
                   return "echo: " + message;
             }
          }
    
          public static JObject Read()
          {
             var stdin = Console.OpenStandardInput();
             var length = 0;
    
             var lengthBytes = new byte[4];
             stdin.Read(lengthBytes, 0, 4);
             length = BitConverter.ToInt32(lengthBytes, 0);
    
             var buffer = new char[length];
             using (var reader = new StreamReader(stdin))
             {
                while (reader.Peek() >= 0)
                {
                   reader.Read(buffer, 0, buffer.Length);
                }
             }
    
             return (JObject)JsonConvert.DeserializeObject<JObject>(new string(buffer));
          }
    
          public static void Write(JToken data)
          {
             var json = new JObject();
             json["data"] = data;
    
             var bytes = System.Text.Encoding.UTF8.GetBytes(json.ToString(Formatting.None));
    
             var stdout = Console.OpenStandardOutput();
             stdout.WriteByte((byte)((bytes.Length >> 0) & 0xFF));
             stdout.WriteByte((byte)((bytes.Length >> 8) & 0xFF));
             stdout.WriteByte((byte)((bytes.Length >> 16) & 0xFF));
             stdout.WriteByte((byte)((bytes.Length >> 24) & 0xFF));
             stdout.Write(bytes, 0, bytes.Length);
             stdout.Flush();
          }
       }
    }
    

    如果您不需要主动与主机通信,使用runtime.sendNativeMessage 即可。要防止主机挂起,只需删除 while 循环并执行一次读/写。

    为了测试这一点,我在这里使用了谷歌提供的示例项目:https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging

    注意:我使用 Json.NET 来简化 json 序列化/反序列化过程。

    我希望这对某人有帮助!

    【讨论】:

    • 我正在搜索 Java 但我在任何地方都找不到:/
    • @farukdgn 具体来说,很难找到什么? Java 实现应该非常相似。
    • 连接在哪里?我尝试运行它,扩展程序给我“本机主机已退出。”
    • 从后台脚本(chrome 扩展)初始化连接时 - 即使打开了 host.exe,连接也会失败。 “连接失败:与本机消息传递主机通信时出错。”根据谷歌link,这是因为错误的序列化/大小声明。有什么线索吗?
    • 为什么不直接创建一个 StreamWriter 并写入 string.Length 和字符串?为什么一定要用WriteByte来写int?
    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多