本机应用程序和 chrome 扩展程序之间的通信 [英] communication between native-app and chrome-extension

查看:34
本文介绍了本机应用程序和 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 扩展程序之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆