HTML and CSS Reference
In-Depth Information
Table 2-1. readyState Attributes, Values, and Status Descriptions
Attribute Constant
Value
Status
WebSocket.CONNECTING
0
The connection is in progress but has not been
established.
WebSocket.OPEN
1
The connection has been established. Messages
can flow between the client and server.
WebSocket.CLOSING
2
The connection is going through the closing
handshake.
WebSocket.CLOSED
3
The connection has been closed or could not be
opened.
(World Wide Web Consortium, 2012)
As the WebSocket API describes, when the WebSocket object is first created, its
readyState is 0 , indicating that the socket is connecting. Understanding the current state
of the WebSocket connection can help you debug your application, such as to ensure
you've opened the WebSocket connection before you've attempted to start sending
requests to the server. This information can also be useful in understanding the lifespan
of your connection.
WebSocket Object Attribute: bufferedAmount
When designing your application, you may want to check for the amount of data buffered
for transmission to the server, particularly if the client application transports large
amounts of data to the server. Even though calling send() is instant, actually transmitting
that data over the Internet is not. Browsers will buffer outgoing data on behalf of your
client application, so you can call send() as often as you like with as much data as you
like. If you want to know how quickly that data is draining out to the network, however,
the WebSocket object can tell you the size of the buffer. You can use the bufferedAmount
attribute to check the number of bytes that have been queued but not yet transmitted to
the server. The values reported in this attribute do not include framing overhead incurred
by the protocol or buffering done by the operating system or network hardware.
Listing 2-17 shows an example of how to use the bufferedAmount attribute to send
updates every second; if the network cannot handle that rate, it adjusts accordingly.
Listing 2-17. bufferedAmount Example
// 10k max buffer size.
var THRESHOLD = 10240;
// Create a New WebSocket connection
var ws = new WebSocket(" ws://echo.websocket.org/updates " );
// Listen for the opening event
ws.onopen = function () {
 
 
Search WWH ::




Custom Search