8

मैं अपने क्रोम एक्सटेंशन और मेरे सी # एप्लिकेशन के बीच मूल संदेश प्राप्त करने का प्रयास कर रहा हूं। जावास्क्रिप्ट ठीक काम करता है, लेकिन मैं इस त्रुटि हो रही है:मूल संदेश क्रोम

Error when communicating with the native messaging host.

आवेदन, विस्तार के साथ शुरू की हो जाता है के रूप में मैं कार्य प्रबंधक से देखा। यहां मेरा सी # कोड है।

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); 
} 

और मुख्य कार्य:

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()); 
    } 
} 

जे एस कोड:

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; 
} 

होस्ट मैनिफ़ेस्ट:

{ 
    "name": "com.example.native", 
    "description": "Native support for Chrome Extension", 
    "path": "NativeApp.exe", 
    "type": "stdio", 
    "allowed_origins": [ 
    "chrome-extension://ajfkjfmkedgcgdckdkmppfblonpeench/" 
    ] 
} 
+0

कोई विचार विचार लोग? –

+0

अपने मूल होस्ट मेनिफेस्ट और जेएस-साइड कोड – Xan

+0

शामिल करें जैसा आपने Xan से पूछा था। कृपया मदद करे। –

उत्तर

22

हाँ कि क्योंकि आप एक गलत डेटा लंबाई भेज है। अपने OpenStandardStreamOut फ़ंक्शन में stringData.Length से msgdata.Length बदलें।

+1

बहुत धन्यवाद !! आखिर में काम करने के लिए मेरी एप मिल गया! –

+9

@ user3692525 कृपया स्वीकार किए गए उत्तर को चिह्नित करें। – Xan

संबंधित मुद्दे