从firefox浏览器插件启动java应用程序时,我甚至在发送消息之前就遇到了一个错误(因此我假设这是一次“握手”)。
我在ubuntu上使用jdk:java-11-openjdk-amd64
我通过sh脚本启动本机应用程序:
# !/bin/sh
set echo off
cd "/home/empeor/work/COLLEGE/final year project/application/NativeApplication/out/artifacts/NativeApplication_jar"
java -Dfile.encoding=UTF-8 -jar NativeApplication.jar
我的test localapplication scala(main)对象是:
object LocalApplication extends App {
val logger = Logger("Local application")
var shook_hands=false
logger.info("native application started")
System.out.println("started application")
var requestJson = "";
while(!requestJson.equals("done")){
requestJson = readMessage(System.in)
logger.info("the message is : " + requestJson)
sendMessage("{'message':'hello''}")
}
logger.info("recieved message says: " + requestJson)
private def readMessage(in: InputStream):String = {
logger.info("trying to read in input")
var b = new Array[Byte](4)
in.read(b,0,4) // Read the size of message
val size = getInt(b)
logger.info("read input length: " + size)
if (size == 0) throw new InterruptedIOException("Blocked communication")
b = new Array[Byte](size)
in.read(b,0,size)
val theMessage = new String(b, "UTF-8")
logger.info("read input message: " + theMessage)
return theMessage
}
@throws[IOException]
private def sendMessage(message: String): Unit = {
System.out.write(getBytes(message.length))
System.out.write(message.getBytes("UTF-8"))
System.out.flush()
}
def getInt(bytes: Array[Byte]): Int =
return ((bytes(3) << 24) & 0xff000000 |
(bytes(2) << 16) & 0x00ff0000 |
(bytes(1) << 8) & 0x0000ff00 |
(bytes(0) << 0) & 0x000000ff)
def getBytes(length: Int):Array[Byte] = {
val bytes = new Array[Byte](4)
bytes(0) = (length & 0xFF).toByte
bytes(1) = ((length >> 8) & 0xFF).toByte
bytes(2) = ((length >> 16) & 0xFF).toByte
bytes(3) = ((length >> 24) & 0xFF).toByte
return bytes
}
System.out.println("ended application")
}
我认为这个“超过字节限制”错误的原因是scala system.in/out编码不正确(firefox本机消息传递使用utf-8)。我是否正确设置了scala中的编码?
确切的错误消息是:
1609255145622 addons.xpi WARN Addon with ID NativeApplication@example.org already installed, older version will be disabled
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable 2 PictureInPictureChild.jsm:298
ExtensionError: Native application tried to send a message of 1918989427 bytes, which exceeds the limit of 1048576 bytes. ExtensionUtils.jsm:58:5
stderr output from native app NativeApplication: Exception in thread "main" java.io.InterruptedIOException: Blocked communication
stderr output from native app NativeApplication: at LocalApplication$.readMessage(LocalApplication.scala:118)
stderr output from native app NativeApplication: at LocalApplication$.delayedEndpoint$LocalApplication$1(LocalApplication.scala:62)
stderr output from native app NativeApplication: at LocalApplication$delayedInit$body.apply(LocalApplication.scala:51)
stderr output from native app NativeApplication: at scala.Function0.apply$mcV$sp(Function0.scala:39)
stderr output from native app NativeApplication: at scala.Function0.apply$mcV$sp$(Function0.scala:39)
stderr output from native app NativeApplication: at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
stderr output from native app NativeApplication: at scala.App.$anonfun$main$1(App.scala:76)
stderr output from native app NativeApplication: at scala.App.$anonfun$main$1$adapted(App.scala:76)
stderr output from native app NativeApplication: at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
stderr output from native app NativeApplication: at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
stderr output from native app NativeApplication: at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
stderr output from native app NativeApplication: at scala.App.main(App.scala:76)
stderr output from native app NativeApplication: at scala.App.main$(App.scala:74)
stderr output from native app NativeApplication: at LocalApplication$.main(LocalApplication.scala:51)
stderr output from native app NativeApplication: at LocalApplication.main(LocalApplication.scala)
只是补充一句:我无法通过以下方式发送消息:
browser.browserAction.onClicked.addListener(() => {
console.log("Sending: ping");
port.postMessage("ping");
});
因为它给出错误:尝试在断开连接的端口上发送postmessage
暂无答案!
目前还没有任何答案,快来回答吧!