user3249448
user3249448

Reputation: 1399

Chrome native message

I am working on sample application of "Chrome Native Messaging". I did all the set as per steps mention on website. I am able to run application as well however i am not getting response message from native application. When i start extension very firts time i get the response message.

Downloaded sample from here

When i sent message from browser native app not responding it check below image

enter image description here

C# code as below

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

    private 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;
    }

    private static void OpenStandardStreamOut(string stringData)
    {
        //// We need to send the 4 btyes of length information
        string msgdata = "{\"text\":\"" + stringData + "\"}";
        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);
    }

manifest.json as below

  {"name": "com.example.native",
  "description": "Native support for Chrome Extension",
  "path": "NativeApp.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ],
  "permissions": [
    "nativeMessaging"
  ]
}

Some where i feel we are not receiving response from native host becoz i have added debug point to following code in browser which is not getting hit

function onNativeMessage(message) {

appendMessage("Received message: " + JSON.stringify(message) + ""); }

Am i missing something ?

Upvotes: 3

Views: 2380

Answers (1)

adam0101
adam0101

Reputation: 30995

I had the same problem. Make sure the message you are sending is valid JSON. In my case, the value being received by the host was "some value" with the double quotation marks. When that was getting concatenated to make the msgdata variable, it was creating invalid JSON.

Upvotes: 2

Related Questions