Windows Chrome internal messaging can only get the first response

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.

+1
source share
1 answer

Try with automatic flushing - std::cout.setf( std::ios_base::unitbuf )

Also, as you read / write the length of the I / O messages, they are incorrect and do not execute 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; } 
+2
source

Source: https://habr.com/ru/post/1201593/


All Articles