Sub*_*esh 8 javascript c# google-chrome google-chrome-extension chrome-native-messaging
我试图在我的chrome扩展和我的c#应用程序之间获取Native Messaging.javascript工作正常,但我收到此错误:
与本机消息传递主机通信时出错.
正如我从任务管理器中看到的那样,应用程序与扩展一起启动.这是我的c#代码.
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 = stringData.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);
}
Run Code Online (Sandbox Code Playgroud)
主要功能:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
JS代码:
var host_name = "com.example.native";
var port = null;
connectToNative();
function connectToNative() {
console.log('Connecting to native host: ' + host_name);
port = chrome.runtime.connectNative(host_name);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
sendNativeMessage("test");
}
function sendNativeMessage(msg) {
message = {"text" : msg};
console.log('Sending message to native app: ' + JSON.stringify(message));
port.postMessage(message);
console.log('Sent message to native app: ' + msg);
}
function onNativeMessage(message) {
console.log('recieved message from native app: ' + JSON.stringify(msg));
}
function onDisconnected() {
console.log(chrome.runtime.lastError);
console.log('disconnected from native app.');
port = null;
}
Run Code Online (Sandbox Code Playgroud)
主机清单:
{
"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "NativeApp.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/"
]
}
Run Code Online (Sandbox Code Playgroud)
Alm*_* G. 24
是的,因为你发送了错误的数据长度.更改stringData.Length
到msgdata.Length
您的OpenStandardStreamOut
功能.
归档时间: |
|
查看次数: |
8159 次 |
最近记录: |