我有一个用c++编写的本地应用程序和一个Chrome扩展程序。
我使用“Chrome原生消息传递”在它们之间通信。
本地应用程序代码:
我正在读取由Chrome扩展程序在标准输入上发送的消息,并通过将其写入标准输出来发送相同的消息。扩展程序使用PostMessage()。这个方法可以工作,但是当我将我的程序放在一个连续的while循环下时,流程只会被执行一次!也就是说,port.postMessage({'text':'hello_1'})会得到预期的回显,但如果我执行port.postMessage({'text':'hello_2'}), 它不会被回显。我无法理解问题所在。它需要线程吗?请帮忙!谢谢!
我使用“Chrome原生消息传递”在它们之间通信。
本地应用程序代码:
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扩展程序在标准输入上发送的消息,并通过将其写入标准输出来发送相同的消息。扩展程序使用PostMessage()。这个方法可以工作,但是当我将我的程序放在一个连续的while循环下时,流程只会被执行一次!也就是说,port.postMessage({'text':'hello_1'})会得到预期的回显,但如果我执行port.postMessage({'text':'hello_2'}), 它不会被回显。我无法理解问题所在。它需要线程吗?请帮忙!谢谢!