JSObject- JavaScript 数组是否作为 Java 数组传递?
我正在尝试将复杂的 Java 小程序转换为 JavaScript。该小程序通过 TCP 套接字进行通信,发挥了各种作用,我需要在 JavaScript 中对其进行模拟。 TCP 魔法本身相当复杂,所以我宁愿稍后再做,先让小程序的图形部分工作。 TCP 套接字上的通信告诉小程序要绘制什么,因此根据套接字上的数据,小程序将读取/写入不同的值。
我当前的攻击计划是:
- 将请求标头作为参数(工作)以及来自套接字的所需数据传递给 JavaScript,具体取决于请求类型
- 解析在 JS 中发送的标头参数/数据并相应地创建对象
- 返回值以使用数组调用 Java 函数要写入 TCP 套接字的字节数
我陷入了最后一部分。如果我返回一个字节数组,如何将其转换为 byte[] 或类似的?传回的值是一个正确的 Java 数组还是某种对象哈希?
Java:
void callJavaScript(Applet app) {
String[] params = {"blah", "cool"};
JSObject win = JSObject.getWindow(app);
Object ret = win.call("someFunction", params);
// what is ret?
}
JavaScript:
function someFunction (blah, cool) {
return [5, 7, 12, 2];
}
注意:
复杂的Java applet 是多线程的,因此仅调用applet 中的某些函数就很重要。
I'm trying to convert a complex Java applet to JavaScript. The applet does all sorts of magic with communicating on a TCP socket, which I will need to emulate in JavaScript. The TCP magic is itself quite complicated, so I'd rather do that later and get the graphical part of the applet working first. The communication over the TCP socket tells the applet what to draw, so depending on the data on the socket, the applet will read/write different values.
My current plan of attack is:
- Pass request header to JavaScript as parameters (working) as well as needed data from socket depending on the request type
- Parse header params/data sent in JS and create objects accordingly
- Return value to calling Java function with an array of bytes to be written to the TCP socket
I'm stuck on the last part. If I return an array of bytes, how can I convert that to a byte[] or similar? Is the value passed back a proper Java array or is it some kind of object hash?
Java:
void callJavaScript(Applet app) {
String[] params = {"blah", "cool"};
JSObject win = JSObject.getWindow(app);
Object ret = win.call("someFunction", params);
// what is ret?
}
JavaScript:
function someFunction (blah, cool) {
return [5, 7, 12, 2];
}
Note:
The complicated Java applet is multi-threaded, so just calling some function in the applet is non-trivial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚写了一个示例来检查 =)
Test.java:
Test.html:
转换显示在 docstore.mik.ua/orelly/web/jscript/ch19_06.html 看起来有效(图 19.5 和 19.6)。您可以在我的代码中看到我检查了
len instanceof Number
而不是len instanceof Double
因为在我的 IE 中它返回了一个Integer
。Did just write a sample to check =)
Test.java:
Test.html:
The conversions show in docstore.mik.ua/orelly/web/jscript/ch19_06.html seen to work(Figures 19.5 and 19.6). You can see in my code I checked
len instanceof Number
and notlen instanceof Double
because in my IE it returned anInteger
.