【问题标题】:Windows Chrome extension native messaging can only receive the first responseWindows Chrome 扩展原生消息只能收到第一响应
【发布时间】:2014-05-26 18:46:12
【问题描述】:

我正在尝试使用本机消息向我的本机 Windows 应用程序发送一些数据。它适用于 runtime.sendNativeMessage() 方法。当我尝试使用使用端口的长期连接时,它还可以将数据从 chrome 传递到我的应用程序。但是,chrome 扩展只能接收来自我的应用程序的第一个响应。我确信端口仍然打开,因为我的应用程序仍然可以从 chrome 接收数据。以下是我的代码:

Chrome 扩展脚本:

var port = chrome.runtime.connectNative('com.mydomain.app1');

port.onMessage.addListener(function(msg) {
    console.log("Received from port:", msg);
});

port.onDisconnect.addListener(function() {
    console.log("Disconnected");
});

chrome.tabs.onUpdated.addListener(
    function(tabId, changeInfo, tab) {  
        var param = {};
        param['url'] = tab.url; 
        port.postMessage( param);   
    }
}

我在 c++ 中的 windows 应用程序:

int _tmain(int argc, _TCHAR* argv[])
{
    while( true )
    {
         //read the first four bytes (=> Length)
         unsigned int length = 0;
         for (int i = 0; i < 4; i++)
         {
              char c;
              if( ( c=getchar()) != EOF) 
                  length += c<<i*8;
              else return 0;
          }

          //read the json-message
          std::string msg = "";
          for (int i = 0; i < length; i++)
          {
              msg += getchar();
          } 

          //.... do something

          //send a response message
          std::string message = "{\"text\": \"This is a response message\"}";
          unsigned int len = message.length();
          // We need to send the 4 bytes 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 << message.c_str();
           std::cout.flush();
          
     }
}   

请注意最后一行“std::cout.flush();”,如果我将其注释掉,即使是第一个响应也不会在 chrome 中显示。我只是不知道 chrome 如何从应用程序的标准输出中读取数据。

【问题讨论】:

  • 只是一个更正:上面的代码实际上是有效的。我的实际代码包含响应消息的一些准备工作。这就是问题所在。请查看我对 Almog 答案的评论。

标签: windows google-chrome-extension chrome-native-messaging


【解决方案1】:

尝试自动冲洗 - std::cout.setf( std::ios_base::unitbuf )

此外,您读/写输入/输出消息长度的方式不正确,并且在长消息时会失败。

这段代码很适合我:

int main(int argc, char* argv[])
{
    std::cout.setf( std::ios_base::unitbuf );

    while (true)
    {
        unsigned int ch, inMsgLen = 0, outMsgLen = 0;
        std::string input = "", response = "";

        // Read 4 bytes for data length
        std::cin.read((char*)&inMsgLen, 4);

        if (inMsgLen == 0)
        {
            break;
        }
        else
        {
            // Loop getchar to pull in the message until we reach the total length provided.
            for (int i=0; i < inMsgLen; i++)
            {
                ch = getchar();
                input += ch;
            }
        }

        response.append("{\"echo\":").append(input).append("}");

        outMsgLen = response.length();

        // Send 4 bytes of data length
        std::cout.write((char*)&outMsgLen, 4);

        // Send the data
        std::cout << response;
    }

    return 0;
}

【讨论】:

  • Almog:非常感谢您的回复。您的代码有效!有了它,我找出了问题的原因。这不是因为自动冲洗。但是在我的响应消息中,在实际代码中,我使用 Json 库来准备字符串,库自动在字符串的末尾添加了一个“\n”,这导致 chrome 认为这是第二个响应的第一个字符我猜。一旦我删除了那个'\ n',它就可以工作了。也感谢您对长度阅读的评论,您的肯定更好。
  • 很遗憾,这对我不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多