问题描述
我做了两个扩展,一个扩展为firefox,一个扩展为chrome(如果有必要发布Chrome代码,请在这里发布,仅告诉我,但非常相似),它有一些动作,调用了native主机应用。其中一个调用返回一条消息,该消息的大小> 1 mb。根据{{3}},“来自应用程序的单个消息的最大大小为1 MB”。根据{{3}},必须将大于1mb的数据分块为少于1mb的数据,并发送尽可能多的块。我制作了一个Java主机应用程序,并提出了对块数据的解决方案。数据被分块,并且发送了许多响应,每个响应在json字符串中都有一个块。发送和响应正常,将消息合并到网页的输入中,并且该操作有效。但是,执行此操作后,其他任何呼叫都会失败。如果我转到Windows任务管理器,并终止本机宿主进程,则一切都会再次起作用。如果我执行得到分块响应的操作,它将再次起作用,但是下一次调用会以静默方式失败,firefox浏览器控制台无法提供任何帮助。我想我的解决方案有问题。动作起作用。当我执行接收到超过1 mb的操作时,该操作有效,但此后没有其他操作再次起作用。任何帮助表示赞赏。告诉我这个问题是否很清楚,或者是一团糟,我会加以改善。我尽了一切努力使它起作用,并且起作用了,但是奇怪的是只有一次,然后它失败了,什么也没做。抱歉,但是我不能说这些动作是什么,我的意思是,我正在尝试做什么,如果确实有必要知道,我会请我的经理在这里进行解释。
Background.js
function sendNativeMessage(message) {
port.postMessage(message);
}
function ondisconnected() {
msg = '{"message": "Failed to connect: '+browser.runtime.lastError+'"}';
browser.tabs.query({active: true,currentwindow: true},function(tabs){
browser.tabs.sendMessage(tabs[0].id,msg,function(response) {});
});
port = null;
}
function connect() {
var hostName = "com.companyname.hostname";
port = browser.runtime.connectNative(hostName);
port.onMessage.addListener(function(msg) {
browser.tabs.query({active: true,function(tabs){
browser.tabs.sendMessage(tabs[0].id,JSON.stringify(msg),function(response) {});
});
});
port.ondisconnect.addListener(ondisconnected);
}
browser.runtime.onMessageExternal.addListener(
function(request,sender,sendResponse) {
if (port == null) {
connect();
}
sendNativeMessage(request);
});
Inject.js
//Prevent code being injected twice(probably because of click listeners on web page)
if (document.readyState !== "complete") {
browser.runtime.onMessage.addListener(function(msg,sendResponse) {
var obj = JSON.parse(msg);
if (!obj.success) {
(...)
}
if (obj.action == "doSomething") {
if (obj.last == "true") {
do something
}else {
var value = $("#input").val().trim() + obj.value.trim();
$("#input").val(value.trim());
}
}
var s = document.createElement('script');
// Todo: add "script.js" to web_accessible_resources in manifest.json
s.src = browser.runtime.getURL('script.js');
s.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(s);
}
script.js
class extensionName{
getValues() {
window.postMessage({ type: "FROM_PAGE",text: {action: "getValues"} },"*");
}
//This is the function that returns more than 1 mb.
dosomething(parameter1,parameter2,parameter3,parameter4) {
window.postMessage({ type: "FROM_PAGE",text: {action: "dosomething",parameter1Key:parameter1,parameter2Key:parameter2,parameter3Key:parameter3,parameter4Key:parameter4} },"*");
}
}
Java中的本地应用程序(aux是辅助类的实例,该类具有sendMessage函数,该函数根据本地消息传递协议发送消息)
switch (parameter1) {
case "getValues":
json = functions.getValues();
aux.sendMessage(json);
break;
case "doSomething":
funcoes.doSomething(...);
break;
(...)
public String doSomething(...) {
(...)
Matcher m = Pattern.compile(".{1,565048}").matcher(valueMoreThan1Mb);
String chunk = "";
while(m.find()) {
System.setout(originalStream);//let stream output values
chunk = newSignatureBase64.substring(m.start(),m.end());
//a json is done here,with needed parameters,action,chunked value.
json = "{\"success\":\"true\",\"action\":\"sign\",\"last\":\"false\",\"value\":\""+chunk+"\"}";/Sorry for this. This code will be improved when everything is working.
aux.sendMessage(json);//auxiliar class sends message according to native messaging protocol.
System.setout(dummyStream);//avoid 3rd party library output contents and mess native messaging port.
}
System.setout(originalStream);//let stream output values
json = "{\"success\":\"true\",\"last\":\"true\"}";//Sorry for this. This code will be improved when everything is working.
aux.sendMessage(json);
System.setout(dummyStream);
return "";
}
Auxiliar.java
public class Auxiliar {
public int getInt(byte[] bytes) {
return (bytes[3] << 24) & 0xff000000 | (bytes[2] << 16) & 0x00ff0000
| (bytes[1] << 8) & 0x0000ff00 | (bytes[0] << 0) & 0x000000ff;
}
public String readMessage(InputStream in) throws IOException {
byte[] b = new byte[4];
in.read(b);
int size = getInt(b);
//log(String.format("The size is %d",size));
if (size == 0) {
throw new InterruptedioException("Blocked communication");
}
b = new byte[size];
in.read(b);
return new String(b,"UTF-8");
}
public boolean sendMessage(String message) {
try {
System.out.write(getBytes(message.length()));
System.out.write(message.getBytes("UTF-8"));
System.out.flush();
return true;
} catch (IOException e) {
// Todo Auto-generated catch block
e.printstacktrace();
return false;
}
}
//Use this in case the other method get deprecated
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;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)