WebSocket Object Properties

Source: Internet
Author: User

One: WebSocket Object properties

You can use a variety of WebSocket object attributes to provide more information about WebSocket objects: ReadyState, Bufferedamount, and protocol.

1. WebSocket Object Characteristics: ReadyState

The WebSocket object reports its connection status through the read-only feature readystate, and you have learned a little bit about it in the previous sections. This property automatically changes depending on the connection state and provides useful information about the WebSocket connection.

Table 2-1 describes the 4 different values used to describe the readystate attribute of the connection state.

Table 2-1 readystate characteristics, values, and status descriptions


Source of information: World Wide Web Consortium, 2012.

As the WebSocket API describes, when the WebSocket object is first created, the readystate is 0, indicating that the socket is connecting. Understanding the current state of the WebSocket connection can help debug your application, such as ensuring that the WebSocket connection has been opened before attempting to start sending a request to the server. This information is also useful for understanding the life cycle of a connection.

Two WebSocket object characteristics: Bufferedamount

When designing an application, you may want to check the amount of buffered data destined to the server, especially when the client application sends a large amount of data to the server. Although the call to send () takes effect immediately, the data is not transmitted over the Internet. The browser caches the outbound data for your client application, allowing you to call send () at any time, sending any amount of data. However, if you want to know the rate at which data is transmitted over the network, the WebSocket object can tell you the size of the cache. You can use the Bufferedamount feature to check the number of bytes that have entered the queue but have not yet been sent to the server. The value reported by this feature does not include the protocol group frame overhead or the buffering performed by the operating system or network hardware.

Listing 2-17 shows an example of sending updates per second using the Bufferedamount feature. If the network is unable to withstand this rate, it will make adjustments accordingly.

Code Listing 2-17 Bufferedamount Example

The Bufferedamount feature is useful for restricting the rate at which applications send data to the server, thus avoiding network saturation.

Expert tips You can check the Bufferedamount attribute of an object before attempting to close the connection to determine if some data has not been sent from the app to the server.

Three WebSocket object characteristics: protocol

In the previous discussion of the WebSocket constructor, we mentioned the protocol parameter, which lets the server know the protocol that the client understands and can use on the websocket. The protocol attribute of the WebSocket object provides another useful piece of information about the WebSocket instance. The results of client and server protocol negotiation can be seen on the WebSocket object. The Protocol attribute contains the protocol name selected by the WebSocket server during the open handshake, in other words, the Protocol feature tells you the protocol used on a particular websocket. The protocol feature is empty until the initial handshake is complete, and if the server does not select a protocol provided by the client, the attribute retains a null value.

WebSocket Object Properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.