C#本地主机与Chrome的Native消息 [英] C# native host with Chrome Native Messaging

查看:2451
本文介绍了C#本地主机与Chrome的Native消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我花了几个小时研究如何让Chrome原始消息用C#本地主机的工作。从概念上讲这是很简单,但也有我的帮助(部分),从这些其他问题解决的几个碰壁:

I spent a few hours today researching how to get Chrome native messaging working with a C# native host. Conceptually it was quite simple, but there were a few snags that I resolved with help (in part) from these other questions:

本地消息Chrome浏览器结果
<一href=\"http://stackoverflow.com/questions/27643892/native-messaging-from-chrome-extension-to-native-host-written-in-c-sharp\">Native从Chrome扩展消息传递在C#编写结果天然宿主
<一href=\"http://stackoverflow.com/questions/27653624/very-slow-to-pass-large-amount-of-data-from-chrome-extension-to-host-written\">Very慢速通过&QUOT;大&QUOT;到主机(用C#编写)的Chrome扩展的数据量

我的解决方案张贴在下面。

My solution is posted below.

推荐答案

假设清单设置正确,这里是使用端口的方法说给C#主机完整的例子:

Assuming the manifest is set up properly, here is a complete example for talking to a C# host using the "port" method:

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 将正常工作。以prevent从悬挂主机,只需删除,而循环和不读/写一次。

If you don't need to actively communicate with the host, using runtime.sendNativeMessage will work fine. To prevent the host from hanging, simply remove the while loop and do Read/Write once.

要测试这一点,我使用由谷歌此处提供的示例项目:<一href=\"https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging\">https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging

To test this, I used the example project provided by Google here: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging

请注意:我使用Json.NET简化JSON序列化/反序列化进程

Note: I'm using Json.NET to simplify the json serialization/de-serialization process.

我希望这是有帮助的人!

I hope this is helpful to somebody!

这篇关于C#本地主机与Chrome的Native消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆