Google chrome extension 如何在本机应用程序中处理chrome.runtime.sendNativeMessage()

Google chrome extension 如何在本机应用程序中处理chrome.runtime.sendNativeMessage(),google-chrome-extension,Google Chrome Extension,我正在使用本机消息传递主机。我可以使用api启动我的自定义应用程序 var port = chrome.runtime.connectNative('com.my_company.my_application'); port.postMessage({ text: "Hello, my_application" }); 我可以使用api将消息发布到我的自定义应用程序 var port = chrome.runtime.connectNative('com.my_company.my_appl

我正在使用本机消息传递主机。我可以使用api启动我的自定义应用程序

var port = chrome.runtime.connectNative('com.my_company.my_application');
port.postMessage({ text: "Hello, my_application" });
我可以使用api将消息发布到我的自定义应用程序

var port = chrome.runtime.connectNative('com.my_company.my_application');
port.postMessage({ text: "Hello, my_application" });
我知道他们正在使用输入/输出流发送和接收消息。 我的本地应用程序(C或C++ exe)应该如何收到有关消息的通知
我应该处理哪个函数/事件来接收消息。

更新:

关于如何监听本机应用程序上的消息,它们会被发送到stdio(目前这是Chrome扩展和本机应用程序之间唯一可用的通信通道)。 请看一看这个以本机消息主机为特色的


您可以在端口的上侦听注册侦听器的消息

仅当需要一次性通信(而非永久端口)时,才使用。在这种情况下,不要使用
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);
    }
});
var msg={…};
chrome.runtime.sendNativeMessage(“),msg,函数(响应){
if(chrome.runtime.lastError){
log(“错误:+chrome.runtime.lastError.message”);
}否则{
log(“消息传递主机sais:,响应”);
}
});

关于>/St>>的DOCS部分是非常详细的,也是一个很好的信息来源。

< P>我发布了C++代码,它将通信,接收并发送消息到Chrome扩展。 希望这能对其他开发者有所帮助

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;
}
int-tmain(int-argc,_-TCHAR*argv[]
{

CUT.SETF(STD:IOSKBASE::UNITBUF);/或“谢谢答复”。我想知道我的本地应用程序(C或C++ exe)如何接收Chrome。RunTime.SeNNATIVEMESATEAGE()API发送的消息。在本地应用程序中,我应该处理哪个事件来获取此消息。我应该在哪里得到我的本地应用程序(C或C++ exe)中的消息?@Nik:我很可能误解了这个问题。请看我的更新答案。@Marc谢谢你的帮助。。我想知道的最后一件事是什么时候我应该关闭.exe。我应该在选项卡关闭时发送自定义消息还是有其他事情?你可以检查“getchar()返回EOF。如果是,则将循环保留为出口。上面的代码和注释都不正确-消息的前4个字节是本机字节顺序的32位无符号整数。例如,请参阅