检测postMessage是否可以发送对象? [英] Detect whether postMessage can send objects?

查看:615
本文介绍了检测postMessage是否可以发送对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种整洁的方式来检测浏览器中的postMessage是否支持发送和接收对象或只是字符串。我认为有一个人必须写出一些这样做的东西,但是我没有找到解决方案。

I'm looking for a neat way to detect whether postMessage in the browser supports the sending and receiving of objects or just strings. I figure that someone out there must have wrote something that does this but I have not managed to find a solution.

我正在使用postMessage将数据发送到/从WebWorker。虽然检测浏览器是否支持工作人员是直接的,但检测对象是否可以通过postMessage发送证明更困难。

I'm using postMessage to send data to/from a WebWorker. Whilst detecting whether the browser supports workers is straight-forward, detecting whether objects can be send via postMessage has proved more difficult.

我想编写一个简单的检测功能。所以,如果浏览器支持发送对象来使用它。如果只允许字符串,我可以回退使用JSON.stringify()。我可能会将该函数分配给dojo /有测试(尽管这与问题/答案无关)。

I'd like to write a simple detection function. So, if the browser supports the sending of objects to use that. If only strings are allowed I can fallback to using JSON.stringify(). I'll probably assign the function to a dojo/has test (although this is not relevant to the question/answer).

其他人做了什么来解决这个问题?任何建议都会很棒,我对WebWorkers和postMessage都是新手。感谢提前。

What have other people done to solve this problem? Any advice would be great, I'm new to both WebWorkers and postMessage. Thanks in advance.

推荐答案

我发现一个更简单的方法来检测postMessage是否只支持字符串或支持其他类型。只需在对象上添加一个自定义toString方法即可。当尝试在IE8和IE9中发送带有postMessage的对象时,它们将被转换为对象上带有toString方法的字符串。因为支持发送对象的浏览器不会调用toString,所以我们可以利用这个来实现我们的优势。这个测试不是异步的,所以你会立即得到结果。没有使用网络工作人员进行测试,但我想您可以使用相同的技术。

I found an even easier way to detect if postMessage only supports strings or if it supports other types. Simply add a custom toString-method on the object. When trying to send an object with postMessage in IE8 and IE9 they will be converted to a string with the toString-method on the object. Since browsers that support sending objects doesn't call toString we can use this to our advantage. This test is not async, so you'll get the result instantly. Haven't tested this with web-workers, but I suppose you can use the same technique.

var onlyStrings = false;
try{window.postMessage({toString:function(){onlyStrings=true;}},"*");}catch(e){}

console.log("Browser only supports postMessage with strings? " + onlyStrings);

在IE8,IE9,IE10和最新版本的Chrome,Firefox,Safari和Opera :
http://jsbin.com/igowoFaj/1/edit?js,console

Tested in IE8, IE9, IE10 and latest version of Chrome, Firefox, Safari and Opera: http://jsbin.com/igowoFaj/1/edit?js,console

更新: BrowserScope test 还有更多的测试和浏览器。结论是,如果 onlyStrings false ,则可以安全地发送可克隆的对象,数组,数字,像素数据和数组缓冲区。理论上,允许发送对象的所有浏览器都应该使用结构化克隆算法 ,但Android浏览器和Opera Mobile都有怪癖。 BrowserScope测试结果有点难以阅读,因为如果浏览器实际上支持该类型,那么send_xxx的0只有问题,因此也请检查supports_xxx。如果他们是相等的,它是确定的,但如果浏览器支持但无法发送(当只有StStings为false时),这是一个错误。

Update: Did a BrowserScope test with many more tests and browsers. Conclusion is that it's safe to send clonable objects, arrays, numbers, pixel data and array buffers if onlyStrings is false. In theory all browsers that allow sending objects should use the structured clone algorithm, but the Android browser and Opera Mobile has quirks. The BrowserScope test result is a bit hard to read, because a 0 for send_xxx is only problematic if the browser actually has support for that type, so check supports_xxx too. If they are equal it's ok, but it's a bug if the browser has support but can't send (when onlyStrings is false).

这篇关于检测postMessage是否可以发送对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆