我正在使用Chrome的本机消息在Chrome扩展程序和C#EXE之间进行通信。可以连接到主机(exe)并向每个主机发送消息,但是主机(C#exe)无法从chrome扩展程序接收数据。
不可能访问正在运行的进程进行调试。因此,我确实将所有数据写入了文件以进行跟踪。但是代码在代码中停止或循环,在代码中将数据格式从流更改为字符串。
->使用(var reader = new StreamReader(stdin))
然后我发现主机从扩展名接收到有线的json数据,如下所示:
{"data":"\u0007\u0000\u0000\u0000\"hello\""}
我猜它在json中出现了一些控制字符。我该如何解决这些问题?
C#
public static void Main(string[] args)
{
JObject data;
//data = Read();
//fileWrite(JsonConvert.SerializeObject(data));
while ((data = Read()) != null)
{
var processed = ProcessMessage(data);
Write(processed);
if (processed == "exit")
{
return;
}
}
}
public static string ProcessMessage(JObject data)
{
var message = data["message"].Value<string>();
switch (message)
{
case "test":
return "testing!";
case "exit":
return "exit";
default:
return "echo: " + message;
}
}
public static JObject Read()
{
var stdin = Console.OpenStandardInput();
var length = 0;
var lengthBytes = new byte[2048];
//stdin.Read(lengthBytes, 0, 100);
int outputLength = stdin.Read(lengthBytes, 0, 2048);
char[] chars = Encoding.UTF7.GetChars(lengthBytes, 0, outputLength);
Write(new string(chars));
length = BitConverter.ToInt32(lengthBytes, 0);
var buffer = new char[length];
using (var reader = new StreamReader(stdin))
{
while (reader.Peek() >= 0)
{
string s = new string(buffer);
reader.Read(buffer, 0, buffer.Length);
}
}
return (JObject)JsonConvert.DeserializeObject<JObject>(new string(buffer))["data"];
}
public static void Write(JToken data)
{
var json = new JObject();
json["data"] = data;
var bytes = System.Text.Encoding.UTF8.GetBytes(json.ToString(Formatting.None));
var stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)((bytes.Length >> 0) & 0xFF));
stdout.WriteByte((byte)((bytes.Length >> 8) & 0xFF));
stdout.WriteByte((byte)((bytes.Length >> 16) & 0xFF));
stdout.WriteByte((byte)((bytes.Length >> 24) & 0xFF));
stdout.Write(bytes, 0, bytes.Length);
stdout.Flush();
}
public static void fileWrite(string text)
{
using (StreamWriter outputFile = new StreamWriter(@".\New_TEXT_File.txt", true))
{
outputFile.WriteLine(text);
}
}
Background.js
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var port = null;
var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
function appendMessage(message) {
console.log(message);
}
function sendNativeMessage(message) {
port.postMessage(message);
appendMessage("Sent message: " + JSON.stringify(message));
}
function onNativeMessage(message) {
appendMessage("Received message: " + JSON.stringify(message));
}
function onDisconnected() {
appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
port = null;
}
function connect() {
var hostName = "com.google.chrome.example.echo";
appendMessage("Connecting to native messaging host" + hostName)
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
document.addEventListener('keydown', function(event){
alert(event.keyCode);
} );
}
window.onload = connect();
manifest.json
{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Native Messaging Example",
"version": "1.0",
"manifest_version": 2,
"description": "Send a message to a native application.",
"background": {
"scripts": ["background.js"]
},
"icons": {
"128": "icon-128.png"
},
"permissions": [
"nativeMessaging"
]
}
它应该正在发送和接收来自chrome扩展程序的所有数据。