Windows Chrome 扩展 native 消息只能收到第一个响应

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

我正在尝试使用 native 消息传递将一些数据发送到我的 native Windows 应用程序。它适用于 runtime.sendNativeMessage() 方法。当我尝试使用使用端口的长期连接时,它也可以将数据从 chrome 传递到我的应用程序。但是,chrome 扩展程序只能收到来 self 的应用程序的第一个响应。我确信该端口仍然打开,因为我的应用程序仍然可以从 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 如何从应用程序的标准输出中读取数据。

最佳答案

尝试使用自动刷新 - 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;
}

关于Windows Chrome 扩展 native 消息只能收到第一个响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23876066/

相关文章:

windows - 在哪里可以找到 mongodb Windows 和 mongodb Linux 之间的对应关系列表?

javascript - 如何确定 Chrome 扩展程序是否位于内容脚本的弹出窗口中?

c++ - 通讯时 Chrome native 消息错误

google-chrome - 与 Firefox 中 Chrome 的 native 客户端消息传递类似的技术?

java - 为 Hadoop 环境配置 Eclipse(在 Windows 上)(在具有 SUSE Linux Enterprise Server 11 的虚拟机上)

python - 使用 Python 在 Windows 10 上列出本地运行的服务?

windows - 如何以融合风格禁用焦点边框和背景QTreeWidget?

google-chrome - Google Chrome 扩展程序和 NPAPI

google-chrome - 将 "chrome_url_overrides"添加到 chrome 扩展,禁用现有用户的扩展?

javascript - 使用 Firefox native 消息进行异步 webRequest.onBeforeRequest URL 检查