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

我正在使用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] <> 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. 

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

我认为你必须交换定义你的消息长度的字节的顺序。 将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; } 

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

发生错误时

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

首先,尝试从命令行启动应用程序,输出可能是“垃圾”