5

私はC++で書かれたネイティブアプリとクロムエクステンションを持っています。native-appとchrome-extensionの間の通信

私はそれらの間で 'chrome native messaging'を使用して通信しています。

ネイティブアプリコード:

int main(int argc, char* argv[]) { 
unsigned int a, c, i, t=0; 
std::string inp; 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(); 
    } 

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

// Collect the length of the message 
unsigned int len = inp.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 <<std::endl; 
flushall(); 
}while(cnt < 2); 
return 0; } 

ここで私は標準入力にクロームの拡張によって送信されたメッセージを読んでいます。それをstdoutに書き込んで同じメッセージを送り返します。

拡張は、()

これが機能している

...しかし..私はwhileループの連続の下で私のプログラムを置く

、フローは一度だけ実行されるのPostMessageを使用しています!

すなわちport.postMessage({ 'テキスト': 'hello_1は'})期待通りにエコーバックされますが、私は

port.postMessage({ 'テキスト': 'hello_2を'})を行う場合、それはありませんエコーバック。

問題の内容を理解できません。スレッディングが必要ですか?

助けてください!

ありがとうございます!

答えて

10

マルクの答えは(質問から継承)多少の誤差が含まれており、1バイトに収まらない長さのメッセージに対しては動作しません。

Chromeのプロトコルは、ネイティブアプリケーションと通信するときには、次のとおりです。

  • ChromeはWindowsにうまく対処していない標準出力を介して送信されているネイティブアプリへ

    • 要求は標準入力を通じてChromeに
    • 応答を受信して​​いますスタイル\ r \ nそのように、メッセージ内でそれを避け、標準入力モードをバイナリに設定します(lenを正しく読み込んで\ nが\ r \ nに変換されないようにします):

      _setmode(_fileno(stdin),_O_BINARY); 
      

    要求および応答メッセージは、メッセージの長さを含む4バイトのヘッダ(UINT32)とJSONである: [長さ4バイトのヘッダ] [メッセージ]

    がリクエストヘッダを読む:

    uint32_t reqLen = 0; 
    cin.read(reinterpret_cast<char*>(&reqLen) ,4); 
    

    応答ヘッダを書き込む:

    cout.write(reinterpret_cast<char*>(&responseLen),4); 
    
  • +0

    親愛なる@bkdc、質問があります。ネイティブメッセージングの種類は何ですか?それはクロームアプリですか、拡張機能ですか?つまり、拡張機能にネイティブメッセージングサンプルを含めることは可能ですか? –

    +0

    @ H.Aqjnこの場合、拡張子です! – rohitvk

    +0

    @ H.Aqjnネイティブメッセージングは​​、npapi/npruntimeプラグインと多少似た役割を果たします。これは、ブラウザができることの限界を「外に出て」他の機能を実装する方法を提供することです。ただし、ネイティブメッセージングは​​、そのような種類の機能のサブセットのみを提供するため、npapi/npruntimeを直接的に置き換えるものではありません。ネイティブメッセージング(APIとして)は、Chrome拡張機能とChromeアプリケーションの両方で、通信プロトコルを実装する外部アプリケーションからデータを送受信するために使用できます。 – bkdc

    2

    これは私の作品:

    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; 
    
    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(); 
        } 
    
        // Loop getchar to pull in the message until we reach the total 
        // length provided. 
        for (i=0; i < t; i++) { 
        c = getchar(); 
        inp += c; 
        } 
    
    //Collect the length of the message 
    unsigned int len = inp.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; 
    } 
    

    ...

    +0

    thnx man!出来た! std :: cout.setf(std :: ios_base :: unitbuf)を追加します。 [私が間違っていれば私を訂正] std :: coutの直後にEOFが必要でしたか? – rohitvk

    +0

    素晴らしい!いいえ、 "eof"は必要ありません。 "std :: ios_base :: unitbuf":各挿入操作の後に出力をフラッシュします。 – Marc

    +5

    @Marcメッセージの長さを読み取るコードが間違っています。バイト値を加算すると正しい長さが返されません。私の回答または[Chromium-extensions forum](https://groups.google.com/a/chromium.org/forum/#!msg/chromium-extensions/Y5RckbPVnr8/xe5w9RC6O5sJ)で提供されるソリューションで回答を編集することを検討してください。 ) 詳細については。 – bkdc

    0

    文字列の長さデコードアルゴリズムが正しくありません。これは修正です:

    for (i = 0; i <= 3; i++) { 
        c = getchar(); 
        l |= (c << 8*i); 
    } 
    
    関連する問題