【问题标题】:Native Messaging host tried sending a message that is 977472013 bytes long本机消息传递主机尝试发送一条长度为 977472013 字节的消息
【发布时间】:2015-08-09 22:04:50
【问题描述】:

我正在使用 java jar 使用 Chrome Native Messaging 发送和接收消息。

我启用了 Chrome 的日志记录,因此我可以读取 C:\Users\%UserName%\AppData\Local\Google\Chrome\User Data\chrome_debug.log 文件

我实际上无法使用我的 Java 应用程序发送或接收消息,但我知道它已被使用。

这是我主机的清单:

{
   "allowed_origins" : 
    [ 
        "chrome-extension://EXTENSION-ID/" 
    ],
   "description" : "my.app",
   "name" : "my.app",
   "path" : "launch.bat",
   "type" : "stdio"
}

这是批处理文件launch.bat的内容:

java -jar "%~dp0ChromeSEOConnector.jar"

这是我的 Java 代码:

private String readMessage(InputStream in) throws IOException {
        byte[] b = new byte[4];
        in.read(b);

        int size = getInt(b);

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

        return new String(b, "UTF-8");
    }

private void sendMessage(String message) throws IOException {
        Text text = new Text(message);
        String resposta = serializer.toJson(text);
        System.out.write(getBytes(resposta.length()));
        System.out.write(resposta.getBytes("UTF-8"));
        System.out.flush();
    }

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

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

似乎 System.in 永远不会获得我的应用程序的输入,而 System.out 也永远不会发送数据。

Chrome 不断出现同样的错误:

Native Messaging host tried sending a message that is 977472013 bytes long.

奇怪的是,消息的大小总是一样的,即使我手动更改发送的消息的大小,就好像根本没有分析消息一样。你遇到过这种错误吗?提前致谢

【问题讨论】:

    标签: java google-chrome chrome-native-messaging


    【解决方案1】:

    我认为您必须交换定义消息长度的字节顺序。将您的 getBytes() 方法更改为:

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

    【讨论】:

      【解决方案2】:

      许多答案建议检查处理前四个字节的正确性。但这并不总是真正的原因。在您的情况下,原因似乎是 launch.bat 中没有 @echo off

      发生错误时

      Native Messaging 主机尝试发送 977472013 字节的消息 长

      首先,尝试从命令行启动应用,有可能输出的是“trash”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多