2016-02-10 17 views
1

我是新手用Chrome扩展,所以我需要通过使用本地消息传递通信信息用C#的.exe应用程序现在如何发送/接收浏览器扩展程序的本地消息传递和C#应用程序之间的消息

问题开发Chrome扩展:我可以连接延伸件(chrome.runtime.connectNative(主机名))之间用C#,但我不能发送/接收消息

这里是我的代码

main.js

// Copyright 2013 The Chromium Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file. 

var port = null; 

var getKeys = function(obj){ 
    var keys = []; 
    for(var key in obj){ 
     keys.push(key); 
    } 
    return keys; 
} 


function appendMessage(text) { 
    document.getElementById('response').innerHTML += "<p>" + text + "</p>"; 
} 

function updateUiState() { 
    if (port) { 
    document.getElementById('connect-button').style.display = 'none'; 
    document.getElementById('input-text').style.display = 'block'; 
    document.getElementById('send-message-button').style.display = 'block'; 
    } else { 
    document.getElementById('connect-button').style.display = 'block'; 
    document.getElementById('input-text').style.display = 'none'; 
    document.getElementById('send-message-button').style.display = 'none'; 
    } 
} 

function sendNativeMessage() { 
    message = {"text": document.getElementById('input-text').value}; 
    port.postMessage(message); 
    appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>"); 
} 

function onNativeMessage(message) { 
    appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>"); 
} 

function onDisconnected() { 
    appendMessage("Failed to connect: " + chrome.runtime.lastError.message); 
    port = null; 
    updateUiState(); 
} 

function connect() { 
    var hostName = "com.google.chrome.example.echo"; 
    appendMessage("Connecting to native messaging host <b>" + hostName + "</b>") 
    port = chrome.runtime.connectNative(hostName); 
    port.onMessage.addListener(onNativeMessage); 
    port.onDisconnect.addListener(onDisconnected); 
    updateUiState(); 
} 

document.addEventListener('DOMContentLoaded', function() { 
    document.getElementById('connect-button').addEventListener(
     'click', connect); 
    document.getElementById('send-message-button').addEventListener(
     'click', sendNativeMessage); 
    updateUiState(); 
}); 

APP.EXE - C#

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 

namespace sign 
{ 
    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["message"].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))["data"]; 
     } 

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

上传.json

// Copyright 2013 The Chromium Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file. 

{ 
    "name": "com.google.chrome.example.echo", 
    "description": "Chrome Native Messaging API Example Host", 
    "path": "native-messaging-example-host.bat", 
    "type": "stdio", 
    "allowed_origins": [ 
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" 
    ] 
} 

.BAT

@echo off 

start %~dp0/Debug/sign.exe 

.manifest.json

{ 
    // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik 
    "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB", 
    "name": "Native Messaging Example", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "Send a message to a native application.", 
    "app": { 
    "launch": { 
     "local_path": "main.html" 
    } 
    }, 
    "icons": { 
    "128": "icon-128.png" 
    }, 
    "permissions": [ 
    "nativeMessaging" 
    ] 
} 

请咨询我一些提示.. 非常感谢你

+0

您是否尝试过阅读本堆栈溢出的票吗? http://stackoverflow.com/questions/27643892/native-messaging-from-chrome-extension-to-native-host-written-in-c-sharp或http://stackoverflow.com/questions/27643892/native-从chrome-extension-to-native-host-messaging-in-c-sharp的消息传递 –

+0

请阅读[本文](http://stackoverflow.com/questions/30880709/c-sharp-native-host-with -chrome-native-messaging),我曾经参考过它并成功建立了扩展和本地应用程序之间的连接 –

回答

0

这是正确的.json

{ 
    "name": "com.google.chrome.example.echo", 
    "description": "Chrome Native Messaging API Example Host", 
    "path": "native-messaging-example-host.bat", 
    "type": "stdio", 
    "allowed_origins": [ 
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" 
    ], 
    "permissions": [ 
    "nativeMessaging" 
    ] 
} 
相关问题