原生应用和Chrome扩展之间的通信 [英] communication between native-app and chrome-extension

查看:397
本文介绍了原生应用和Chrome扩展之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C语言编写本机应用程序++和铬扩展。

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

我使用铬本地消息他们之间的通信。

I am communicating between them using 'chrome native messaging'.

本机应用程序code:

Native-App code:

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;  }

由铬扩展标准输入送到这里,我读的消息。并通过写它在标准输出上发送相同的消息回来了。

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({'文本':'hello_1'})被回显如预期,但如果我这样做

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

port.postMessage({'文本':'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'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浏览器的响应是通过标准输出发送

  • 浏览器不使用Windows风格的\\ r \\处理好ñ所以要避免在消息并设置标准输入模式为二进制(这样你就可以正确读取请求LEN和\\ n不转进\\ r \\ n)的:

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

_setmode(_fileno(stdin),_O_BINARY);


的请求和响应消息是JSON用含有该消息的长度为4字节的头(UINT32):
[长度为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天全站免登陆