如何在chrome扩展程序和本机应用程序之间维持长期连接?

时间:2019-02-19 06:56:43

标签: c# multithreading google-chrome-extension console-application chrome-native-messaging

我正在尝试使Chrome扩展程序与本机应用程序(C#控制台应用程序)保持长期连接。如果单击我的扩展选项卡上的“刷新”图标,则控制台应用程序将不会被激活,并且port.onDisconnect.addListener()事件将被触发。从查看视图->背景页面中,我可以看到:

    message: "Native host has exited."

Chrome扩展程序可以从C#控制台应用程序获取消息,而C#控制台应用程序可以从chrome扩展程序接收消息。控制台应用程序中有一个事件,一旦触发事件,信息就会通过Write()发送到chrome扩展程序。如果我手动运行控制台应用程序,则事件触发时我可以在控制台应用程序中查看信息。

控制台应用程序:

    static void Main(string[] args)
    {
        string message = "test message from native application";
        Write(message);
        //Read();
        Thread thread = new Thread(Read);
        thread.Start();
        ScanInit();
    }


    public static void Write(string data)
    {
        var json = new JObject();
        json["data"] = data;
        var bytes = Encoding.UTF8.GetBytes(json.ToString(Formatting.None));
        int DataLength = bytes.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) 

        stdout.Write(bytes, 0, bytes.Length);
        //stdout.Flush();
    }

    public static void Read()
    {
        //// 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();
        }

        //Write(input);
        return input;
    }

background.js

    var host_name = "myhost"; 
    var port = null; 

    var newport = connectToNative(); 

    function connectToNative() { 
        console.log('Connecting to native host: ' + host_name); 
        port = chrome.runtime.connectNative(host_name); 

        port.onMessage.addListener(onNativeMessage); 

        port.onDisconnect.addListener(onDisconnected); 
        sendNativeMessage("Message from chrome extension."); 

        return port;
    } 

    function sendNativeMessage(msg) { 
        var message = {data : msg}; 
        console.log('Sending message to native app: ' + JSON.stringify(message)); 
        port.postMessage(message); 
        console.log('Sent message to native app: ' + msg); 
    } 

    function onNativeMessage(msg) { 
        //alert('received messge from native app.');
        console.log(chrome.runtime.lastError);
        console.log('recieved message from native app: ' + JSON.stringify(msg)); 

        return true;
    } 

    function onDisconnected() { 
        console.log(chrome.runtime.lastError); 
        console.log('disconnected from native app.'); 
        port = null; 
    } 

我想使用chrome激活控制台应用程序,并保持长期连接。然后在必要时使用port.disconnect()。以下是Inspect视图背景页面:

    Connecting to native host: myhost
    Sending message to native app: {"data":"Message from chrome extension."}
    Sent message to native app: Message from chrome extension.

    undefined
    recieved message from native app: {"data":"test message from native application"}
    Objectmessage: "Native host has exited."
    get message: ƒ ()
    __proto__: Object
    disconnected from native app.

更新第一个: 修改代码后,似乎长期连接有效。但它不适用于控制台应用程序中的事件处理程序。

static void Main(string[] args)
{
    string message = "test message from native application";
    Write(message);
    //Read();
    string data;
    while ((data = Read()) != null) // (Read() != null || Read() != "")
    {
         //Write("Received to Native App: " + Read());
         Write("Recieved: " + data);
    }

}

0 个答案:

没有答案