I am trying to use integrated messaging to send some data to my own windows application. It works well with the runtime.sendNativeMessage () method. When I try to use long-lived connections that use the port, it can also transfer data from chrome to my application. However, the chrome extension can only get the first response from my application. I am sure that the port is still open, because my application can still receive data from chrome. Below is my code:
Chrome Script Extension:
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 application 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(); } }
Please note that the last line is “ std :: cout.flush (); ”, if I comment on this, even the first answer will not be displayed in chrome. I just couldn't figure out how chrome reads from a stdout application.
source share