Windows Chrome扩展本地消息只能接收第一个响应 [英] Windows Chrome extension native messaging can only receive the first response
问题描述
我试图使用本地消息发送一些数据到我的本地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(
函数(tabId,changeInfo,tab){
var param = {};
param ['url'] = tab.url;
port.postMessage(param);
}
}
$ p
$ b
int _tmain(int argc,_TCHAR * argv [])
{
while(true)
{
//读取前四个字节(=>长度)
unsigned int length = 0;
for(int i = 0; i <4; i ++)
{
char c;
if((c = getchar())!= EOF)
length + = c< i * 8;
else返回0;
}
//读取json-message
std :: string msg =;
for int i = 0; i< length; i ++)
{
msg + = getchar();
}
// ....做某事
//发送回复消息
std :: string message ={\text \:\这是一个响应信息\};
unsigned int len = message.length();
//我们需要发送4个字节的长度信息
std :: cout<< char(((len>> 0)& 0xFF))
<< char(((len>> 8)& 0xFF))
<< char(((len>> 16)& 0xFF))
<< char(((len>> 24)& 0xFF));
//现在我们可以输出我们的消息
std :: cout<< message.c_str();
std :: cout.flush();
注意最后一行 strong> std :: cout.flush(); ,如果我注释掉它,即使第一个响应也不会显示在chrome中。我只是无法弄清楚chrome如何从应用程序的stdout中读取数据。
试着用自动刷新 - 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 =;
//为数据长度读取4个字节
std :: cin.read((char *)& inMsgLen,4);
if(inMsgLen == 0)
{
break;
}
else
{
//循环getchar来拉取消息,直到达到所提供的总长度。
for(int i = 0; i< inMsgLen; i ++)
{
ch = getchar();
input + = ch;
response.append({\echo \:)。append(input).append(});
outMsgLen = response.length();
//发送4字节的数据长度
std :: cout.write((char *)& outMsgLen,4);
//发送数据
std :: cout<<响应;
}
返回0;
}
I am trying to use native messaging to send some data to my native windows application. It works well with the runtime.sendNativeMessage() method. When I am trying to use long lived connections which uses a port, it also can pass data from chrome to my app. However, chrome extension can only receive the first response from my app. I am sure that the port is still open because my app can still receive data from chrome. Following are my code:
Chrome Extension Script:
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);
}
}
My windows app in c++:
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();
}
}
Notice that the last line "std::cout.flush();", if I comment it out, even the first response won't be shown in chrome. I just couldn't figure out how chrome reads from the app's stdout.
解决方案 Try with automatic flushing - std::cout.setf( std::ios_base::unitbuf )
Also, the way you read/write the input/output messages length is incorrect and will fail on long messages.
This code works well for me:
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扩展本地消息只能接收第一个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!