简体   繁体   English

如何从Java applet线程调用JavaScript回调?

[英]How to call a JavaScript callback from a Java applet thread?

I have some long-running Java code running in a thread started by a Java applet. 我在一个由Java applet启动的线程中运行了一些长时间运行的Java代码。 As soon as the code has finished, it has information for the user. 代码完成后,它就会为用户提供信息。 I'd like to pass this information to a JavaScript callback in a thread-safe way. 我想以线程安全的方式将此信息传递给JavaScript回调。 Just using the Java DOM API to modify the HTML document is not good enough, unless my JavaScript callback gets called as a side effect in a thread safe way (ie eventually, in the same browser thread where JavaScript callbacks execute). 仅使用Java DOM API来修改HTML文档是不够的,除非我的JavaScript回调以线程安全的方式被调用为副作用(即最终,在执行JavaScript回调的同一浏览器线程中)。 How can I implement this? 我该如何实现呢? Are there libraries already doing it? 图书馆已经在做了吗?

Looks as though the Common DOM API is what you need. 看起来好像您需要Common DOM API。 In essence you request a DOMService to call you back on the main UI thread when it is good and ready. 本质上,您请求DOMService在主UI空间线程良好且准备就绪时回调您。

http://docs.oracle.com/javase/1.5.0/docs/guide/plugin/developer_guide/java_js.html http://docs.oracle.com/javase/1.5.0/docs/guide/plugin/developer_guide/java_js.html

As I understand things, JSObject is the "old way" and the Common DOM API is the newer way (as of Java 6). 据我所知,JSObject是“旧方式”,而Common DOM API是更新的方式(从Java 6开始)。 What you need to do is call a method of your applet and pass the function object that you want to have called back. 你需要做的是调用applet的方法并传递你想要回调的函数对象。 Your applet then calls that function object from within your DOMAction. 然后,您的applet从DOMAction中调用该函数对象。

I have no code at hand to show you and this is not something I've done before using Java. 我没有代码向您展示,这不是我在使用Java之前所做的事情。 However I have used a similar technique from with an NPAPI web plugin written in C++ ie had JS call my C++ object passing a function as a parameter, and then at a later stage, have the C++ object call the JS function. 但是我使用了一个用C ++编写的NPAPI web插件的类似技术,即JS调用我的C ++对象将函数作为参数传递,然后在稍后阶段,让C ++对象调用JS函数。 'hope that this helps. '希望这会有所帮助。

I had concurrency issues with multiple applet threads calling Javascript. 我有多个applet线程调用Javascript的并发问题。 Some of the calls were simply ignored by Internet Explorer 8 (Firefox 3.6 and Safari 5 worked fine). Internet Explorer 8忽略了一些调用(Firefox 3.6和Safari 5工作正常)。

I tried to wrap calls to Javascript with DOMService.invokeAndWait - that didn't help. 我试图用DOMService.invokeAndWait包装对Javascript的调用 - 这没有帮助。

I ended up ensuring that all the Java->Javascript calls are made from the same thread, and my problems were solved (at least temporarily - I haven't checked what happens if the Javascript thread is busy with some user-initiated work). 我最终确保所有Java-> Javascript调用都来自同一个线程,并且我的问题得到了解决(至少暂时 - 我还没有检查如果Javascript线程忙于某些用户启动的工作会发生什么)。

To serialize the calls, I used Executors.newSingleThreadExecutor 为了序列化调用,我使用了Executors.newSingleThreadExecutor

It seems that there is no public tutorial how to do it. 似乎没有公共教程如何做到这一点。 The whole JavaScript--Java interface is underdocumented, and it is subject to browser limitations. 整个JavaScript-Java接口未被记录,并且受浏览器限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM