Javascript webextension本机应用程序c++;你好,世界 < P> >我想在Firefox上创建一个WebFirefox,它与一个简单的C++应用程序通信,我想向C++应用程序发送一个消息,并接收一个简单的消息(Hello World,例如)。

Javascript webextension本机应用程序c++;你好,世界 < P> >我想在Firefox上创建一个WebFirefox,它与一个简单的C++应用程序通信,我想向C++应用程序发送一个消息,并接收一个简单的消息(Hello World,例如)。,javascript,c++,firefox,firefox-addon,nativeapplication,Javascript,C++,Firefox,Firefox Addon,Nativeapplication,现在,这就是我所做的: manifest.json(webextension) background.js var port = browser.runtime.connectNative("nameofapp"); console.log("Sending: ping"); port.postMessage("ping"); port.onMessage.addListener((response) => { console.log("Received: " + response)

现在,这就是我所做的:

manifest.json(webextension)

background.js

var port = browser.runtime.connectNative("nameofapp");
console.log("Sending:  ping");
port.postMessage("ping");
port.onMessage.addListener((response) => {
  console.log("Received: " + response);
});
manifest.json(c++应用程序)

main.cpp

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    //cin.get();
    return 0;
}

我在这里找到了答案:

#包括
int main(int argc,char*argv[])
{
std::cout.setf(std::ios_base::unitbuf);//而不是“
{
  "name": "nameofapp",
  "description": "Example host for native messaging",
  "path": "bin/Debug/nameofapp.exe",
  "type": "stdio",
  "allowed_extensions": [ "name@example.org" ]
}
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    //cin.get();
    return 0;
}
Sending:  ping  
Native application tried to send a message of 1819043144 bytes, 
which exceeds the limit of 1048576 bytes.
#include <iostream>

int main(int argc, char* argv[])
{
    std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
    unsigned int a, c, i, t=0;
    std::string inp;
    bool bCommunicationEnds = false;

    do {

        inp="";
        t=0;
        // Sum the first 4 chars from stdin (the length of the message passed).
        for (i = 0; i <= 3; i++)
        {
//          t += getchar();
            t += std::cin.get();
        }
        // Loop getchar to pull in the message until we reach the total
        //  length provided.
        for (i=0; i < t; i++)
        {
            //c = getchar();
            c = std::cin.get();
            //if(c == EOF)
            if(c == 65)
            {
                bCommunicationEnds = true;
                i = t;
            }
            else
            {
                inp += c;
            }
        }

        if(!bCommunicationEnds)
        {
            //Collect the length of the message
             unsigned int len = inp.length();
            //unsigned int len = strJson.length();
            //// We need to send the 4 btyes of length information
            std::cout << char(((len>>0) & 0xFF))
                << char(((len>>8) & 0xFF))
                << char(((len>>16) & 0xFF))
                << char(((len>>24) & 0xFF));
            //// Now we can output our message
            std::cout << inp;

        }
    }while(!bCommunicationEnds);
    return 0;
}