请问Ja​​vaScript的WebSocket.send方法块? [英] Does JavaScript WebSocket.send method block?

查看:675
本文介绍了请问Ja​​vaScript的WebSocket.send方法块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要送一个大的的Blob ArrayBuffer 在一个JavaScript 的WebSocket 通过其发送方法...请问发送方法调用块,直到数据发送呢,还是使数据的副本,以异步发送,电话可立即返回?

一个相关的(未)的问题是,从如何我间preT它,一系列快速的发送是否会导致延迟的onMessage事件,有人似乎已经描述了移动Safari浏览器发生了什么:<一href=\"http://stackoverflow.com/questions/17219963/apparent-blocking-behaviour-in-javascript-websocket-on-mobile-safari\">Apparent在JavaScript中的WebSocket移动Safari浏览器阻止行为


解决方案

根据在 bufferedAmount 属性,我推断的说明 发送 必须立即返回,否则 bufferedAmount 会始终是零。如果是不为零,那么就必须从以前的调用缓存发送数据,如果发送缓冲区数据,没有理由为它挡住。

http://dev.w3.org/html5/websockets/


  

bufferedAmount 属性必须返回的字节数
  已排队的应用程序数据(UTF-8文本和二进制数据)
  使用发送()但是,随着最近一次事件循环开始
  执行任务,还没有被发送到网络。 (这个
  因此包括当前任务的执行过程中发送的任何文字,
  不管用户代理是否能够传输文本
  与异步脚本执行)。这还不包括框架
  由协议开销发生时,或由操作缓冲完成
  系统或网络硬件。如果连接被关闭,这
  属性值将只与每个调用的增加发送()
  方法(数不​​重置为零一旦连接闭合)。


  
  

在这个简单的例子,在 bufferedAmount 属性是用来保证
  该更新无论是在一个更新每隔50ms,如果的速率发送
  网络能够处理这样的速度,或者在任何速率网络可以
  处理,如果是太快了。


  VAR插座=新的WebSocket(WS://game.example.com:12010 /更新');
socket.onopen =功能(){
    的setInterval(函数(){
       如果(socket.bufferedAmount == 0)
           socket.send(getUpdateData());
    },50);
};


  

bufferedAmount 属性还可以用于网络饱和
  没有以更高的速率比网络可以处理发送数据,
  虽然这需要随着时间的属性的值的更仔细的监测。


If I'm sending a large Blob or ArrayBuffer over a JavaScript WebSocket via its send method... does the send method call block until the data is sent, or does it make a copy of the data to send asynchronously so the call can return immediately?

A related (unanswered) question is, from how I interpret it, whether a rapid series of sends will cause onmessage events to be delayed, as someone seems to have described happening in Mobile Safari: Apparent blocking behaviour in JavaScript websocket on mobile Safari

解决方案

Based on the description of the bufferedAmount attribute, I have deduced that send must return immediately, because otherwise bufferedAmount would always be zero. If it is non-zero, then there must be data buffered from a prior call to send, and if send buffers data, there's no reason for it to block.

From http://dev.w3.org/html5/websockets/

The bufferedAmount attribute must return the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but that, as of the last time the event loop started executing a task, had not yet been transmitted to the network. (This thus includes any text sent during the execution of the current task, regardless of whether the user agent is able to transmit text asynchronously with script execution.) This does not include framing overhead incurred by the protocol, or buffering done by the operating system or network hardware. If the connection is closed, this attribute's value will only increase with each call to the send() method (the number does not reset to zero once the connection closes).

In this simple example, the bufferedAmount attribute is used to ensure that updates are sent either at the rate of one update every 50ms, if the network can handle that rate, or at whatever rate the network can handle, if that is too fast.

var socket = new WebSocket('ws://game.example.com:12010/updates');
socket.onopen = function () {
    setInterval(function() {
       if (socket.bufferedAmount == 0)
           socket.send(getUpdateData());
    }, 50);
};

The bufferedAmount attribute can also be used to saturate the network without sending the data at a higher rate than the network can handle, though this requires more careful monitoring of the value of the attribute over time.

这篇关于请问Ja​​vaScript的WebSocket.send方法块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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