我正在研究“ Chrome Native Messaging ”的示例应用程序。我按照网站上提到的步骤完成了所有设置。我也可以运行应用程序,但是我没有从本机应用程序收到响应消息。当我第一次开始扩展时,我会收到响应消息。
当我从浏览器本机应用程序发送消息没有响应时,请检查下图
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"
]
}
有些地方我觉得我们没有收到来自本机主机的响应,因为我已经在浏览器中的以下代码中添加了调试点,这些代码没有被命中
function onNativeMessage(message) {
appendMessage("收到的消息:" + JSON.stringify(message) + " "); }
我错过了什么吗?