• <i id='YpvVY'><tr id='YpvVY'><dt id='YpvVY'><q id='YpvVY'><span id='YpvVY'><b id='YpvVY'><form id='YpvVY'><ins id='YpvVY'></ins><ul id='YpvVY'></ul><sub id='YpvVY'></sub></form><legend id='YpvVY'></legend><bdo id='YpvVY'><pre id='YpvVY'><center id='YpvVY'></center></pre></bdo></b><th id='YpvVY'></th></span></q></dt></tr></i><div id='YpvVY'><tfoot id='YpvVY'></tfoot><dl id='YpvVY'><fieldset id='YpvVY'></fieldset></dl></div>

    <tfoot id='YpvVY'></tfoot>
  • <legend id='YpvVY'><style id='YpvVY'><dir id='YpvVY'><q id='YpvVY'></q></dir></style></legend>

        <bdo id='YpvVY'></bdo><ul id='YpvVY'></ul>

      1. <small id='YpvVY'></small><noframes id='YpvVY'>

      2. 本机应用程序和 chrome 扩展程序之间的通信

        时间:2023-06-29

          <i id='RKLM3'><tr id='RKLM3'><dt id='RKLM3'><q id='RKLM3'><span id='RKLM3'><b id='RKLM3'><form id='RKLM3'><ins id='RKLM3'></ins><ul id='RKLM3'></ul><sub id='RKLM3'></sub></form><legend id='RKLM3'></legend><bdo id='RKLM3'><pre id='RKLM3'><center id='RKLM3'></center></pre></bdo></b><th id='RKLM3'></th></span></q></dt></tr></i><div id='RKLM3'><tfoot id='RKLM3'></tfoot><dl id='RKLM3'><fieldset id='RKLM3'></fieldset></dl></div>
          <legend id='RKLM3'><style id='RKLM3'><dir id='RKLM3'><q id='RKLM3'></q></dir></style></legend>

            <tfoot id='RKLM3'></tfoot>
              <tbody id='RKLM3'></tbody>

                <bdo id='RKLM3'></bdo><ul id='RKLM3'></ul>

                <small id='RKLM3'></small><noframes id='RKLM3'>

                1. 本文介绍了本机应用程序和 chrome 扩展程序之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个用 C++ 编写的本机应用程序和一个 chrome 扩展.

                  I have a native app written in c++ and a chrome-extension.

                  我正在使用chrome 原生消息传递"在他们之间进行通信.

                  I am communicating between them using '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;  }
                  

                  在这里,我正在阅读标准输入上 chrome-extension 发送的消息.并通过将其写入 stdout 来发送回相同的消息.

                  Here I'm reading message sent by chrome-extension on stdin. and sending the same message back by writing it on stdout.

                  扩展正在使用 PostMessage()

                  Extension is using PostMessage()

                  这是有效的...但是..

                  This is working... BUT ..

                  当我将程序置于连续 while 循环中时,流程只执行一次!

                  When I put my program under continuous while loop, the flow executes only once!

                  即 port.postMessage({'text':'hello_1'}) 按预期得到回显,但如果我这样做了

                  i.e port.postMessage({'text':'hello_1'}) gets echoed back as expected but if I do

                  port.postMessage({'text':'hello_2'}) 它不会被回显.

                  port.postMessage({'text':'hello_2'}) it doesn't get echoed back.

                  我无法理解问题是什么.需要穿线吗?

                  I'm unable to understand what the problem is. Does it require threading?

                  请帮忙!

                  谢谢!

                  推荐答案

                  Marc 的回答包含一些错误(从问题继承而来)并且不适用于长度不适合一个字节的消息.

                  Marc's answer contains some errors (inherited from the question) and will not work for messages with lengths that do not fit in one byte.

                  Chrome 的协议,当与原生应用通信时:

                  Chrome's protocol, when communicating with native apps is:

                  • 通过标准输入接收对原生应用的请求
                  • 对 Chrome 的响应通过标准输出发送
                  • Chrome 不能很好地处理 Windows 样式 所以在消息中避免这种情况并将 stdin 模式设置为二进制(这样你就可以正确读取请求 len 并且 不会变成" ):

                  • requests to native app are received through stdin
                  • responses to Chrome are sent through stdout
                  • Chrome doesn't deal well with Windows style so avoid that in the messages and set stdin mode to binary (so you can correctly read the request len and doesn't 'turn' into ):

                  _setmode(_fileno(stdin),_O_BINARY);
                  

                  请求和响应消息是带有包含消息长度的 4 字节标头 (uint32) 的 JSON:[长度 4 字节头][消息]

                  The request and response messages are JSON with a 4 byte header (uint32) containing the length of the message: [length 4 byte header][message]

                  读取请求头:

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

                  写入响应头:

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

                  这篇关于本机应用程序和 chrome 扩展程序之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以在 Google Chrome 扩展程序中嵌入 HTTP 服务器? 下一篇:C++ 中图形问题的邻接表或邻接矩阵哪个更好?

                  相关文章

                  1. <i id='yeZAv'><tr id='yeZAv'><dt id='yeZAv'><q id='yeZAv'><span id='yeZAv'><b id='yeZAv'><form id='yeZAv'><ins id='yeZAv'></ins><ul id='yeZAv'></ul><sub id='yeZAv'></sub></form><legend id='yeZAv'></legend><bdo id='yeZAv'><pre id='yeZAv'><center id='yeZAv'></center></pre></bdo></b><th id='yeZAv'></th></span></q></dt></tr></i><div id='yeZAv'><tfoot id='yeZAv'></tfoot><dl id='yeZAv'><fieldset id='yeZAv'></fieldset></dl></div>
                      <bdo id='yeZAv'></bdo><ul id='yeZAv'></ul>
                    <tfoot id='yeZAv'></tfoot>

                    <small id='yeZAv'></small><noframes id='yeZAv'>

                      <legend id='yeZAv'><style id='yeZAv'><dir id='yeZAv'><q id='yeZAv'></q></dir></style></legend>