How to handle chrome.runtime.sendNativeMessage () in a native application

I am working on my own messaging server. I can run my own application using api

var port = chrome.runtime.connectNative('com.my_company.my_application');

I can send a message to my user application using api

port.postMessage({ text: "Hello, my_application" });

I know that they use an input / output stream to send and receive messages. how my native application (c or C ++ exe) will receive a notification about the received message which function / event I must process to receive the message.

+2
source share
2 answers

I am sending C ++ code that will transmit ie, receive and send messages on chrome extension. Hope this helps another developer.

int _tmain(int argc, _TCHAR* argv[])
{
    cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
    _setmode(_fileno(stdin),_O_BINARY);


    unsigned int c, i, t=0;
    string inp;  
    bool bCommunicationEnds = false;

    bool rtnVal = true;
    do {

        inp="";
        t=0;
        //Reading message length 
        cin.read(reinterpret_cast<char*>(&t) ,4);

        // Loop getchar to pull in the message until we reach the total
        //  length provided.
        for (i=0; i < t; i++) {
            c = getchar();
            if(c == EOF)
            {
                bCommunicationEnds = true;
                i = t;
            }
            else
            {
                inp += c;
            }
        }

         if(!bCommunicationEnds)
        {
            //Writing Message length
            cout.write(reinterpret_cast<char*>(&inp),4); 
            //Write original message.
            cout<<inp;
        }
    }while(!bCommunicationEnds);
    return 0;
}
+3
source

UPDATE:

, , stdio ( Chrome ). , python.


, onMessage .

sendNativeMessag(), ( ). chrome.runtime.connectNative(...). :

var msg = {...};
chrome.runtime.sendNativeMessage("<your_host_id>", msg, function(response) {
    if (chrome.runtime.lastError) {
        console.log("ERROR: " + chrome.runtime.lastError.message);
    } else {
        console.log("Messaging host sais: ", response);
    }
});

docs Native Messaging .

+3

Source: https://habr.com/ru/post/1538712/


All Articles