我正在开展示例应用" Chrome原生消息"。我根据网站上提到的步骤完成了所有设置。我也可以运行应用程序但是我没有从本机应用程序获得响应消息。当我第一次开始扩展时,我得到了响应消息。
当我从浏览器本机应用程序发送消息而没有响应时,请检查下面的图像
C#代码如下
static void Main(string[] args)
{
string message = "test message from native app.";
OpenStandardStreamOut(message);
while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
{
OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn());
OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn());
}
}
private static string OpenStandardStreamIn()
{
//// We need to read first 4 bytes for length information
Stream stdin = Console.OpenStandardInput();
int length = 0;
byte[] bytes = new byte[4];
stdin.Read(bytes, 0, 4);
length = System.BitConverter.ToInt32(bytes, 0);
string input = "";
for (int i = 0; i < length; i++)
{
input += (char)stdin.ReadByte();
}
return input;
}
private static void OpenStandardStreamOut(string stringData)
{
//// We need to send the 4 btyes of length information
string msgdata = "{\"text\":\"" + stringData + "\"}";
int DataLength = msgdata.Length;
Stream stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
//Available total length : 4,294,967,295 ( FF FF FF FF )
Console.Write(msgdata);
}
manifest.json
如下
{"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "NativeApp.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
],
"permissions": [
"nativeMessaging"
]
}
我认为我们没有收到来自本机主机becoz的响应的一些我已经在浏览器中添加了以下代码的调试点,但没有被点击
function onNativeMessage(message) {
appendMessage(&#34;收到的消息:&#34; + JSON.stringify(消息)+&#34; &#34;); }
我错过了什么吗?
答案 0 :(得分:3)
我遇到了同样的问题。确保您发送的消息是有效的JSON。就我而言,主机收到的值为"some value"
,带有双引号。当它被连接起来制作msgdata
变量时,它就是在创建无效的JSON。