我想通过chrome原生消息传递在chrome扩展和C#应用程序之间创建双向通信

我想通过 chrome 原生消息传递在 chrome 扩展和 C# 应用程序之间创建双向通信。但是为什么Failed to connect: Native host has exited在尝试使用以下代码执行相关的c#应用程序时出现错误。这里有什么问题?

C#代码

 static string OpenStandardStreamIn()
{
    //// We need to read first 4 bytes for length information
    Stream stdin = Console.OpenStandardInput();
    int length = 0;
    byte[] bytes = new byte[4];
    stdin.Read(bytes, 0, 4);
    length = System.BitConverter.ToInt32(bytes, 0);
    string input = "";
    for (int i = 0; i < length; i++)
    {
        input += (char)stdin.ReadByte();
    }
    return input;
}
 static void OpenStandardStreamOut(string stringData)
{
    //// We need to send the 4 btyes of length information
    string msgdata = "{"text":"" + stringData + ""}";

图片[1]-我想通过chrome原生消息传递在chrome扩展和C#应用程序之间创建双向通信-唐朝资源网

int DataLength = msgdata.Length; Stream stdout = Console.OpenStandardOutput(); stdout.WriteByte((byte)((DataLength >> 0) & 0xFF)); stdout.WriteByte((byte)((DataLength >> 8) & 0xFF)); stdout.WriteByte((byte)((DataLength >> 16) & 0xFF)); stdout.WriteByte((byte)((DataLength >> 24) & 0xFF)); //Available total length : 4,294,967,295 ( FF FF FF FF ) Console.Write(msgdata); } static void Main(string[] args) { string message = "test message from native app."; OpenStandardStreamOut(message); while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "") { OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn()); OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn()); } }

我使用此代码创建了一个 exe 文件。

js 代码

var port = null;
function appendMessage(text) {
  document.getElementById('response').innerHTML += '

' + text + '

'; } function updateUi() { 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'; } } window.onload = function () { document.getElementById('connect-button').onclick = connect(); }; function sendNativeMessage() { message = { text: document.getElementById('input-text').value }; port.postMessage(message); appendMessage('Sent message: ' + JSON.stringify(message) + ''); } function getNativeMessage(message) { appendMessage('Received message: ' + JSON.stringify(message) + ''); } function onDisconnected() { appendMessage('Failed to connect: ' + chrome.runtime.lastError.message); if ( chrome.runtime.lastError.message == 'Specified native messaging host not found.' ) { } port = null; updateUi(); }

图片[2]-我想通过chrome原生消息传递在chrome扩展和C#应用程序之间创建双向通信-唐朝资源网

function connect() { var hostName = 'com.poc.myapp'; // Host directory also created in registry appendMessage('Connecting to native messaging host ' + hostName + ''); port = chrome.runtime.connectNative(hostName); port.onMessage.addListener(getNativeMessage); port.onDisconnect.addListener(onDisconnected); updateUi(); } document.addEventListener('DOMContentLoaded', function () { document.getElementById('connect-button').addEventListener('click', connect); document .getElementById('send-message-button') .addEventListener('click', sendNativeMessage); updateUi(); });

app.json

{
    "name": "com.poc.myapp",
    "description": "My Application",

图片[3]-我想通过chrome原生消息传递在chrome扩展和C#应用程序之间创建双向通信-唐朝资源网

"path": "C:\Users\username\Downloads\App_2\setup.exe", "type": "stdio", "allowed_origins": [ "chrome-extension://XXXXXXXXXXXXXXXXXXXXXXXX/" ] }

manifest.json

{
  "name": "Native Messaging Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Send a message to a native application.",
  "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt17ZTIKUsP3T2f/Gs2I4Hb5yOyXojazVw931RNbFZUqlBYWBYTMFlw8wsi6QUhN3yuZnCwDWdK3vH/OxOSyfJ22i8yx+mto3ZMaBMgE4+1YIGioYFYyEjzbpjTusx7VUOKfTTl3GsVWxw29G0DHOzo8X4uEjFL9+9IaiSi4AToDPjfX2ERxKpwX5bFNtRhlKcdpZeZI0vXc24B0BTeH01F6BjUNzgj5WrRsyDg5PqYhPofAGwHMWCUaOaGUzuhroOlgYC7DDcANVxgLt2j/0RIiqfU60AXzcZRJjoQN6wV4IB65bYdx9XxiqUG2CB0nCLk2BV7z3xsTmLkdXDNZTxQIDAQAB",
  "browser_action": {
    "default_icon": "icon-128.png",
    "default_popup": "main.html"
  },
  "icons": {
    "128": "icon-128.png"
  },
  "permissions": ["nativeMessaging"]
}

© 版权声明
THE END
喜欢就支持一下吧
点赞171 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片