如何解决Safari 10.1错误“无法发送WebSocket框架”?

时间:2017-04-03 21:46:37

标签: javascript websocket safari webkit

似乎Safari 10.1中的WebSocket API具有可以缓冲的最大二进制数据量,然后发送的下一条消息会收到错误“WebSocket连接到...失败:无法发送WebSocket帧。”

然后

Safari关闭与代码1006(CLOSE_ABNORMAL)的连接。

WebSockets是supposed to report the bufferedAmount - 但Safari始终报告// this fails in Safari 10.1 but works in 10.03 (and other browsers) var ws = new WebSocket('wss://echo.websocket.org'); ws.onerror = function(evt) { // Not sure why, but error events seem to have no useful information // The console, however, will have the following error: // WebSocket connection to 'wss://echo.websocket.org/' failed: Failed to send WebSocket frame. console.log("WebSocket error - see console for message"); } ws.onclose = function(evt) { console.log(`WebSocket closed with code: ${evt.code}, reason: ${evt.reason}`); } ws.onopen = function() { console.log('sending first binary message'); ws.send(new Uint8Array(23085)); console.log('bufferedAmount is ' + ws.bufferedAmount); // this gets the error console.log('sending second binary message'); ws.send(new Uint8Array(23085)); console.log('bufferedAmount is ' + ws.bufferedAmount); console.log('sending third binary message'); ws.send(new Uint8Array(23085)); console.log('bufferedAmount is ' + ws.bufferedAmount); ws.close(); } ,直到发生错误并且连接关闭后

我尝试在每条消息之间执行100ms的setTimeout,这似乎适用于小块数据的情况,但是当我发送结束JSON消息时它看起来很脆弱且大块仍然会出错,即使有更长的时间延迟。

您可以see the bug in action here - “播放示例”按钮在Safari 10.03中有效,但在10.1中有误。 (Code that handles the WebSocket connection.

有关如何解决此问题的任何想法?或者甚至是限制是什么?我知道Safari是开源的,但我不确定在哪里看。

更新:这是一个更简单的例子:

bufferedAmount

https://jsfiddle.net/yta2mjuf/2/

第二条消息收到错误关闭连接,第三条消息后,background-image: url("https://www.example.com/image.jpg"); background-size: cover; // use different options for different effects background-repeat: no-repeat; background-position: center; height: 80px; width: 80px; 为23093。

1 个答案:

答案 0 :(得分:1)

我在Safari 10.1.2上尝试了你的真实世界链接,并没有看到问题。似乎它已被修复。