javascript - Java and chrome extension, natives messages -


i try chrome extension call java code in pc. call works fine, code executes, try return variables chrome extension don't work. see in console listener ondisconect write console message, listener onmessage don't. don't know problem.

here code in chrome extension:

manifest json

{     "name": "prueba native message",     "version": "1.0",     "manifest_version": 2,     "description": "chrome extension interacting native messaging ,     localhost.",     "app": {     "background": {         "scripts": ["background.js"]     } },     "icons": {     },     "permissions": [         "nativemessaging"     ] } 

background.js

var port = chrome.runtime.connectnative('com.app.native');  function message(msg) {     console.warn("received" + msg); }  function disconect() {     console.warn("disconnected"); }  console.warn("se ha conectado");  port.onmessage.addlistener(message); port.ondisconnect.addlistener(disconect); port.postmessage({text: "hello, my_application"});  console.warn("message send"); 

and here local files.

.bat

cd c:\users\pc\ideaprojects\edni\out\production\code && java main 

main.java

public class main {     public static void main(string argv[]) throws ioexception {         system.out.println("{\"m\":\"hi\"");     } } 

in code try return simple message extension.

native messaging protocol

chrome starts each native messaging host in separate process , communicates using standard input (stdin) , standard output (stdout). same format used send messages in both directions: each message serialized using json, utf-8 encoded and preceded 32-bit message length in native byte order. maximum size of single message native messaging host 1 mb, protect chrome misbehaving native applications. maximum size of message sent native messaging host 4 gb.

source: native messaging protocol

the first 4 bytes need length of message. need convert message length, integer, byte array:

option 1: using java.nio.bytebuffer class

public byte[] getbytes(int length) {     bytebuffer b = bytebuffer.allocate(4);     b.putint(length);     return b.array(); } 

option 2: manual:

public byte[] getbytes(int length) {     byte[] bytes = new byte[4];     bytes[0] = (byte) (length & 0xff);     bytes[1] = (byte) ((length >> 8) & 0xff);     bytes[2] = (byte) ((length >> 16) & 0xff);     bytes[3] = (byte) ((length >> 24) & 0xff);     return bytes; } 

write out message length , message content in bytes.

string message = "{\"m\":\"hi\"}"; system.out.write(getbytes(message.length()));  system.out.write(message.getbytes("utf-8")); system.out.flush(); 

update:

it looks missing interface type needs specified in manifest file.

add this: "type": "stdio"


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -