简体   繁体   English

如何在Web Worker中编码对象以使其通过postMessage传递?

[英]How do you encode an Object in a Web Worker for it to be passed via postMessage?

Internally, Firefox will JSON encode an object passed via postMessage to and from the Web Worker. 在内部,Firefox将对通过postMessage传递给Web Worker和从Web Worker传递的对象进行JSON编码。 However, this only works in Trunk builds of Firefox (3.6+) and not in Firefox 3.5, so the question is really how to add backwards support of this operation to the current platform. 但是,这仅在Firefox(3.6+)的Trunk版本中起作用,而在Firefox 3.5中不起作用,因此问题实际上是如何向当前平台添加对此操作的向后支持。 The window.atob() and window.btoa() methods had been suggested before, but alas these are not available internally to the threads because they don't have access to the DOM. 之前曾建议过window.atob()和window.btoa()方法,但是由于这些线程无法访问DOM,因此它们在线程内部不可用。

Mozilla publicly states this on their developer wiki, but it has been noticed by many in the community that this happens. Mozilla在其开发者Wiki上公开声明了这一点,但是社区中的许多人已经注意到这种情况的发生。 Check ejohn's blog test: http://ejohn.org/files/bugs/postMessage/ 检查ejohn的博客测试: http ://ejohn.org/files/bugs/postMessage/

I've verified that this is the case as well, in 3.5, it passes only strings, and in 3.6 is will pass the object. 我已经验证了这种情况,在3.5中,它仅传递字符串,而在3.6中将传递对象。

I haven't noticed the automatic JSON-encoding not working in Firefox 3.5, but I've mainly been working with Gears, which doesn't support it anyway. 我没有注意到自动JSON编码在Firefox 3.5中不起作用,但是我主要是在Gears上工作,而Gears仍然不支持它。

Try including a JSON utility in both the worker script and the parent script, then manually encode and decode it yourself. 尝试在辅助脚本和父脚本中都包含JSON实用程序 ,然后自己手动对其进行编码和解码。 This works fairly well for me with Gears. Gears对我来说效果很好。

This approach shouldn't break when Firefox begins automatically doing the JSON encoding for you, since the encoded JSON string will remain a string. 当Firefox开始自动为您执行JSON编码时,这种方法应该不会中断,因为编码后的JSON字符串将仍然是字符串。

In JavaScript there are two functions respectively for decoding and encoding base64 strings: 在JavaScript中,有两个函数分别用于解码和编码base64字符串:

atob() btoa() atob()btoa()

由于您显然正在寻找仅使用FF的解决方案,因此您是否尝试过yourObject.toSource()

I found the solution to my own problem! 我找到了解决自己问题的方法!

It seems that if the thread variable, even if declared globally, loses its .onmessage property if that said property was declared inside another function. 如果线程变量即使在全局范围内声明,也可能会丢失其.onmessage属性(如果该属性是在另一个函数中声明的)。 If the property is instantiated on the global scope, then JSON messages are parsed correctly. 如果在全局范围内实例化该属性,则可以正确解析JSON消息。

I'm still not sure I understand what's going on here, but at least I've figured out a way to pass objects around without having to rely on any additional stingify/json libraries. 我仍然不确定我了解这里发生了什么,但是至少我已经找到了一种无需依赖任何其他stingify / json库即可传递对象的方法。

If anyone could explain this to me so I have a better understanding, it would be appreciated :) 如果有人可以向我解释这一点,以便我有更好的理解,将不胜感激:)

I setup a test case here: http://x.miernicki.com/ which logs the inter-thread messages to the firebug console if anyone cares. 我在这里设置了一个测试用例: http : //x.miernicki.com/ ,如果有人关心,它将线程间消息记录到Firebug控制台。 This helped me to get objects passed around in Fox3.5 and ultimately allowed me to see what the problem was. 这帮助我在Fox3.5中传递了对象,并最终使我了解了问题所在。

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

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