如何解决Firefox Web 扩展本地消息发送到 Scala 应用程序超出消息字节限制
从 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
我的测试 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,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,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");
});
因为它给出错误:尝试在断开连接的端口上发布消息
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。