Nik*_*Nik 4 google-chrome-extension
我正在本地消息传递主机上工作。我可以使用 api 启动我的自定义应用程序
var port = chrome.runtime.connectNative('com.my_company.my_application');
Run Code Online (Sandbox Code Playgroud)
我可以使用 api 将消息发布到我的自定义应用程序
port.postMessage({ text: "Hello, my_application" });
Run Code Online (Sandbox Code Playgroud)
我知道他们使用输入/输出流来发送和接收消息。我的本机应用程序(c 或 c++ exe)应该如何获得有关接收到的消息的通知,我应该处理哪个函数/事件来接收消息。
更新:
关于如何监听原生应用上的消息,它们被发送到stdio(目前这是Chrome扩展程序和原生应用之间唯一可用的通信渠道)。看看这个示例应用程序,它具有在 python 中实现的本机消息传递主机。
您侦听在端口onMessage事件上注册侦听器的消息。
sendNativeMessag()仅当您需要一次性通信(不是持久端口)时才使用。在这种情况下,不要使用chrome.runtime.connectNative(...). 相反,请执行以下操作:
var msg = {...};
chrome.runtime.sendNativeMessage("<your_host_id>", msg, function(response) {
if (chrome.runtime.lastError) {
console.log("ERROR: " + chrome.runtime.lastError.message);
} else {
console.log("Messaging host sais: ", response);
}
});
Run Code Online (Sandbox Code Playgroud)
关于本机消息传递的文档部分非常详细,是一个很好的信息来源。
我正在发布将进行通信的 C++ 代码,即接收消息并将消息发送到 chrome 扩展。希望这对其他开发人员有所帮助
int _tmain(int argc, _TCHAR* argv[])
{
cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
_setmode(_fileno(stdin),_O_BINARY);
unsigned int c, i, t=0;
string inp;
bool bCommunicationEnds = false;
bool rtnVal = true;
do {
inp="";
t=0;
//Reading message length
cin.read(reinterpret_cast<char*>(&t) ,4);
// Loop getchar to pull in the message until we reach the total
// length provided.
for (i=0; i < t; i++) {
c = getchar();
if(c == EOF)
{
bCommunicationEnds = true;
i = t;
}
else
{
inp += c;
}
}
if(!bCommunicationEnds)
{
//Writing Message length
cout.write(reinterpret_cast<char*>(&inp),4);
//Write original message.
cout<<inp;
}
}while(!bCommunicationEnds);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7961 次 |
| 最近记录: |