Is the WebSocket.send JavaScript method enabled?

If I send a large Blob or ArrayBuffer via JavaScript WebSocket using the send method ... does it make a call to the send method before sending data or does it make a copy of the data for asynchronous sending so that the call can return immediately?

A related (unanswered) question is that, how I interpret it, whether a hasty series of posts will delay onmessage events, as someone seems to have described what is happening in Mobile Safari: Explicit blocking behavior in JavaScript web browser on mobile Safari

+6
source share
1 answer

Based on the description of the bufferedAmount attribute, I came to the conclusion that send should return immediately, because otherwise the bufferedAmount will always be zero. If it is nonzero, then there must be data buffered from the previous call to send, and if the data of the send buffers are missing, there is no reason to block it.

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

The bufferedAmount attribute should return the number of bytes of the application data (UTF-8 text and binary data) that were queued using send () , but at the same time, the last time the event loop was run, the task has not yet been transferred to the network. (This thus includes any text sent during the execution of the current task, regardless of whether the user agent can transmit the text asynchronously with the script.) This does not include framing the overhead incurred by the protocol, or buffering performed by an existing system or network equipment. If the connection is closed, this attribute value will only increase with each send () call (the number is not reset is zero after the connection is completed).

In this simple example, the bufferedAmount attribute is used to ensure that updates are sent either in the amount of one update every 50 ms if the network can handle this speed or no matter what network can if it 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 data at a faster rate than the network can operate, although this requires more careful monitoring of the attribute value over time.

+3
source

Source: https://habr.com/ru/post/947765/


All Articles