在native-app和chrome-extension之间一段时间后,连接断开 [英] connection breaks after a while between native-app and chrome-extension

查看:179
本文介绍了在native-app和chrome-extension之间一段时间后,连接断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用chrome native消息传递api在我的chrome扩展和用c ++编写的native-windows-app之间进行通信。也得到交换。但是连接在随机数调用之后从扩展到native-app。



我试图独立运行native-app,并且在无限运行时工作正常



我的本​​机应用程序在第一次调用( encode_frame())时生成近300KB的数据,然后连续调用(每300ms)产生0到300KB的数据( encode_frame_difference())。数据以base64编码。



FYI:通过stdin和stdout在本机应用和扩展之间进行通信。



问题是我无法弄清为什么连接会在一段时间后中断。



这里是 / strong>:
windows-native-app-cpp



以下是扩展代码
chrome-extension-js < a>



任何帮助将不胜感激。



谢谢。



EDIT
到目前为止,我发现我发送的特定长度的数据有问题。



p。如果JSON长度在2560和2815之间,它停止工作。而对于JSON长度像2816或6656它工作。

解决方案

我从chrome扩展组和它WORKS为我。



粘贴完全相同:



问题很可能出现在包含四字节长度消息。如果这很奇怪,Chrome会中断连接。由于默认情况下stdout处于文本模式,因此在Windows上某些ASCII字符可能会转换为不同的字符,例如\\\
将变为\r\\\
。您的标头中最多只有您需要的字节,因此Chrome扩展程序会认为您将发送数百万字节的数据,混淆并中断连接,并且某些标头字节流入消息中,导致



尝试将stdout设置为二进制模式:

  _setmode(_fileno(stdout),_O_BINARY); 

如果这样没有帮助,您可以另外尝试这种写入stdout的备用方法: p>

  unsigned int len = final_msg.length(); 
fwrite(& len,4,1,stdout);
printf(%s,final_msg.c_str());
fflush(stdout);

您可能需要添加一些包括:
fcntl.h
io。 h


I am using chrome native messaging api to communicate between my chrome-extension and native-windows-app which is written in c++.

The connection establishes finely and data also gets exchanged. But the connection breaks after random number of calls are made from extension to the native-app.

I tried running native-app independently and it works fine when run in an infinite loop(no exceptions occur).

My native-app generates almost 300KB of data at first call(encode_frame()) and then consecutive calls(per 300 ms) are made that generate 0 to 300KB of data(encode_frame_difference()). Data is base64 encoded.

FYI: communication occur via stdin and stdout between native-app and extension.

The problem is I am not able to figure out why connection breaks after some time.

Here is the native-app code: windows-native-app-cpp

Here is the extension code: chrome-extension-js

Any help would be appreciated!

Thank you.

EDIT: Till now I have found out that there is problem with certain length of data I'm sending.

eg. If the JSON length is between 2560 and 2815, it stops working. while for JSON lengths like 2816 or 6656 it works.

解决方案

I got this from chromium-extension group and it WORKS for me.

Pasting the exact same:

The problem is likely in the header that contains the four-byte length of the message. If it's weird, Chrome will break the connection. Since stdout is in text mode by default, certain ASCII characters may be getting converted to different ones on Windows, such as \n becomes \r\n. You end up with more bytes in the header than you need, so the Chrome extension will think that you're sending millions of bytes of data, gets confused, and breaks the connection, and some of the header bytes bleed into the message, causing the JSON parser to encounter unexpected characters.

Try setting stdout into binary mode:

_setmode(_fileno(stdout), _O_BINARY);

If that doesn't help, you can additionally try this alternate method of writing to stdout:

unsigned int len = final_msg.length();
fwrite(&len, 4, 1, stdout);
printf("%s", final_msg.c_str());
fflush(stdout);

You may need to add some includes: fcntl.h io.h

这篇关于在native-app和chrome-extension之间一段时间后,连接断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆