2016-02-10 44 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" 
    ] 
} 
相關問題