繁体   English   中英

java中如何实现chrome native messaging消息处理协议

[英]How to implement chrome native messaging message handling protocol in java

我尝试在 java 中实现本地消息传递协议,但没有成功。
我按照以下方式尝试了它。

private String readMessage() {

        int length = getInt(getLength());

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        byte[] b = new byte[4];

        try {
            int total;
            for(int totalRead = 0 ; totalRead < length ; totalRead = totalRead + 4){
            System.in.read(b); // make sure
                bOut.write(b);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        String bRes = null;
        try {
            bRes = new String(bOut.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return bRes;

    }

为了读取尺寸,我使用了以下方法:

这从前四个字节构造了 int

    private int getInt(byte[] bytes)
    {
        return (bytes[3] << 24) & 0xff000000 |
                (bytes[2] << 16) & 0x00ff0000 |
                (bytes[1] << 8) & 0x0000ff00 |
                (bytes[0] << 0) & 0x000000ff;
    }

这读取前四个字节并返回字节数组

private byte[] getLength()
        {
            int length = 0 ;
            byte[] bytes = new byte[4];
            try
            {
                 System.in.read(bytes);

            } catch (IOException e)
            {
               e.printStackTrace();
            }


            return bytes;

    }

这会出现“与本机消息传递主机通信时出错”错误。 我怎样才能在 java 中正确地实现这个协议。
有人可以为 java 提供简单的工作示例吗

我下面的方法给出了一个Java实现,该实现从Chrome应用接收消息并向后发送消息。 在我的小端机上,它可以工作。

我没有适当地研究您的工作,但希望这对您的“简单工作示例”要求有所帮助。

要点:与标准流进行通信。 如您所知,分别读取前4个字节以了解长度(此处为lengthByte):

byte[] lengthByte = new byte[4];
int bytesRead = System.in.read(lengthByte,0,4);

//Read the message into byte[] c:
byte[] c = new byte[text_length];
int lengthAppMessage = System.in.read(c,0,text_length);

写回应用程序时,我们将消息长度写在第一个4个字节中。 对于消息{"m":"hi"} ,这是我在下面发送的消息,消息长度为10。(对于{"m":"hello"}为13,依此类推)

int returnedMessageLength = 10;

System.out.write((byte) (returnedMessageLength));
System.out.write((byte)0);
System.out.write((byte)0);
System.out.write((byte)0);

最后三行填充以总计4个字节。 您可能需要将这三行放在消息长度之前的流中。

附加消息时,需要使用{"...":"..."}格式。 我们可以通过以下方式发送消息:

System.out.append('{');
System.out.append('"');
System.out.append('m');
System.out.append('"');
System.out.append(':');
System.out.append('"');
System.out.append('h');
System.out.append('i');
System.out.append('"');
System.out.append('}');  

关键是将消息分成多个部分并分别发送每个部分可以解决Java格式问题(由单引号引起)。

将以上所有代码放入永无止境的“ while”循环中,以免退出时间过早。 (要查看此代码的运行情况,我将其与Google本机消息页面中的示例集成在一起。)

这不是我用过的好代码,但是无论是偶然还是设计,它都对我有效。

以下代码对我而言效果很好。

//Convert length from Bytes to int
    public static int getInt(byte[] bytes) {
        return (bytes[3] << 24) & 0xff000000|
                (bytes[2] << 16)& 0x00ff0000|
                (bytes[1] << 8) & 0x0000ff00|
                (bytes[0] << 0) & 0x000000ff;
    }


// Read an input from Chrome Extension
static public String receiveMessage(){

    byte[] b = new byte[4];

    try{
        System.in.read(b);
        int size = getInt(b);

        byte[] msg = new byte[size];
        System.in.read(msg);

        // make sure to get message as UTF-8 format
        String msgStr = new String(msg, "UTF-8");

        return msgStr;

    }catch (IOException e){
        e.printStackTrace();
        return null;
    }

}

您可以使用https://github.com/Cosium/web-native-messaging-host 它是一个 java 库,允许将任何 JVM 应用程序转换为 Web 本机消息传递主机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM