与本机消息传递主机Java

时间:2019-12-19 18:09:51

标签: javascript java google-chrome-extension

因此,我正在学习使用Java使用chrome扩展的本机消息传递。测试应用程序时出现以下错误:与本机消息传递主机通信时出错。 我不确定是什么问题,或者如何跟踪错误。 这是我的checker.bat文件:

@echo off
javac Test.java
java Test

这是清单。JSON:

{
  "name": "host1",

  "description": "Test",
  "path": "checker.bat",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://eogmgkhgmdjkdhkfacckkjhmffnniiab/"
  ]
}

公共课程测试{

public static void main(String[] args) {
    Test t=new Test();
    String to_process_string="";
    try {
        to_process_string = t.readMessage(System.in);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    String final_output;
    String output=to_process_string+" recieved";
    final_output="{\"m\":\""+output+"\"}";
    try {
        t.sendMessage(final_output);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private String readMessage(InputStream in) throws IOException {
    byte[] b = new byte[4];
    in.read(b);

    int size = getInt(b);

    if (size == 0) {
      throw new InterruptedIOException("Blocked communication");
    }

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

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

  private void sendMessage(String message) throws IOException {
    System.out.write(getBytes(message.length()));
    System.out.write(message.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;
  }

} 我不太确定您能帮我什么问题吗? 注意:这是我注册密钥的方式:REG添加“ HKCU \ Software \ Google \ Chrome \ NativeMessagingHosts \ host1” / ve / t REG_SZ /d“C:\Users\Rex\Desktop\TestNative\Host\manifest.json” / f

编辑:因此,我尝试使用WoXxo在注释中建议的JSON格式,但它仍然给我同样的错误。我决定将问题记录到文件中,但是什么也没记录,所以我决定连接以使主要功能为空,看看是否是代码问题或连接问题。事实证明,它一直给我相同的结果。知道可能是什么问题吗?

0 个答案:

没有答案