admin管理员组文章数量:1295066
I am trying to get Native Messaging between my chrome extension and my c# application. The javascript works fine, but I am getting this error:
Error when communicating with the native messaging host.
The application does get launched along with the extension, as I saw from Task Manager. Here is my c# code.
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);
}
And the main function:
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());
}
}
JS Code:
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;
}
Host Manifest:
{
"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "NativeApp.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/"
]
}
I am trying to get Native Messaging between my chrome extension and my c# application. The javascript works fine, but I am getting this error:
Error when communicating with the native messaging host.
The application does get launched along with the extension, as I saw from Task Manager. Here is my c# code.
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);
}
And the main function:
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());
}
}
JS Code:
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;
}
Host Manifest:
{
"name": "com.example.native",
"description": "Native support for Chrome Extension",
"path": "NativeApp.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/"
]
}
Share
Improve this question
edited Jul 10, 2017 at 5:53
darcyy
5,2765 gold badges29 silver badges42 bronze badges
asked Jun 14, 2014 at 10:49
user1414202user1414202
4362 gold badges7 silver badges22 bronze badges
3
- Include your native host manifest and the JS-side code – Xan Commented Jun 14, 2014 at 11:49
- I did as you asked to Xan. Please help. – user1414202 Commented Jun 14, 2014 at 11:59
- No I got it to work, but I can only send Messages from chrome extension to c# app. If i do vice versa, with the OpenStandardStreamOut method, I get the 'error communicating..' message – user1414202 Commented Jun 14, 2014 at 12:09
1 Answer
Reset to default 28Yeah that's because you send a wrong data length. Change stringData.Length
to msgdata.Length
in your OpenStandardStreamOut
function.
本文标签: cNative Messaging ChromeStack Overflow
版权声明:本文标题:c# - Native Messaging Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738492047a2089763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论