我正在尝试使用本机消息传递在chrome扩展和c#app之间进行通信。我已经提到了这个链接 nativeMessaging
在我的扩展程序中有2个菜单按钮,一个用于连接,另一个用于postmessage。
当我点击connect native时,我的c#host app(app.exe)就会启动。但点击postmessage后,app.exe中没有收到任何消息。
以下是示例代码
com.ln.json
{
"name": "com.ln",
"description": "Chrome Native Messaging API Example Host",
"path": "app.bat",
"type": "stdio",
"allowed_origins":
[
"chrome-extension://hplkgmmknmccnfalidkjhkponfakmhag/"
]
}
menupopup.js
var port=null;
chrome.browserAction.onClicked.addListener(LinguifyOnClick());
function MenuButtonClicked(evnt)
{
try
{
var menuId = evnt.target.id;
if(menuId == "0")
{
Communicate();
}
if(menuId == "1")
{
SendPostMessage();
}
window.close();
}
catch(err)
{
}
}
function LinguifyOnClick()
{
try
{
//var BodyTag = document.getElementsByTagName("body");
var DefaultTag = document.getElementById("DefaultUl");
for (iCtr = 0; iCtr<3; iCtr++)
{
var tmpTag = document.createElement("li");
tmpTag.id = iCtr.toString();
tmpTag.className = iCtr.toString();
if(iCtr==0)
{
tmpTag.innerHTML = " ConnectToNativeHost";
}
else if(iCtr==1)
{
tmpTag.innerHTML = " SendPostMessage";
}
DefaultTag.appendChild(tmpTag);
tmpTag.addEventListener('click', MenuButtonClicked);
}
}
catch(err)
{
}
}
function SendPostMessage()
{
var message = {"text" : "PostMessageSent_EXT"};
alert("SendPostMessage=" + port + JSON.stringify(message));
port.postMessage(message);
alert("SendPostMessage=" + JSON.stringify(message));
}
function Communicate()
{
port = chrome.runtime.connectNative('com.ln');
//port = chrome.extension.connectNative('com.ln');
port.onMessage.addListener(function(msg)
{
alert("Received from native app:1 =" + msg);
});
port.onDisconnect.addListener(function()
{
alert("Disconnected");
});
}
app.exe(c#代码)
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
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)
{
try
{
var message = data["message"].Value<string>();
switch (message)
{
case "test":
return "testing!";
case "exit":
return "exit";
default:
return "echo: " + message;
}
}
catch (Exception ex)
{
Console.Write("ProcessMessage =" + ex.Message);
}
return " ";
}
public static JObject Read()
{
try
{
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));
}
catch (Exception ex)
{
Console.Write("Read =" + ex.Message);
}
return null;
}
public static void Write(JToken data)
{
try
{
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();
}
catch (Exception ex)
{
Console.Write("Write =" + ex.Message);
}
}
}
}
答案 0 :(得分:0)
我现在能够沟通。我改变的是