C# Chrome本机消息
我正在开发“Chrome本机消息传递”的示例应用程序。我按照网站上提到的步骤完成了所有设置。我也能够运行应用程序,但我并没有从本机应用程序收到响应消息。当我第一次启动扩展时,我会收到响应消息 当我从浏览器本机应用发送消息时,没有响应,请检查下图 C#代码如下C# Chrome本机消息,c#,google-chrome,chrome-native-messaging,C#,Google Chrome,Chrome Native Messaging,我正在开发“Chrome本机消息传递”的示例应用程序。我按照网站上提到的步骤完成了所有设置。我也能够运行应用程序,但我并没有从本机应用程序收到响应消息。当我第一次启动扩展时,我会收到响应消息 当我从浏览器本机应用发送消息时,没有响应,请检查下图 C#代码如下 static void Main(string[] args) { string message = "test message from native app."; OpenStandardSt
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);
}
有些地方我觉得我们并没有收到来自本机主机的响应,因为我在浏览器中为下面的代码添加了调试点,但并没有收到响应
function onNativeMessage(message) {
appendMessage(“收到的消息:+JSON.stringify(消息)+”);
}
我遗漏了什么吗?我也有同样的问题。确保您发送的消息是有效的JSON。在我的例子中,主机接收的值是带有双引号的“some value”。当连接到
msgdata
变量时,它创建了无效的JSON。我得到:“Uncaught TypeError:chrome.runtime.connectNative不是函数”@user34660,这是可能的,或者他在我发表评论后删除了他的评论。不管怎样,现在都没什么意义了。我将删除我的评论。
function onNativeMessage(message) {