Tutorials Point, Simply Easy Learning
HTML5 – WebSockets
Web Sockets is a next-generation bidirectional communication technology for web applications
which operates over a single socket and is exposed via a JavaScript interface in HTML 5
compliant browsers.
Once you get a Web Socket connection with the web server, you can send data from browser to
server by calling a send() method, and receive data from server to browser by an onmessage
event handler.
Following is the API which creates a new WebSocket object.
var Socket = new WebSocket(url, [protocal] );
Here first argument, url, specifies the URL to which to connect. The second attribute, protocol is
optional, and if present, specifies a sub-protocol that the server must support for the connection
to be successful.
WebSocket Attributes:
Following are the attribute of WebSocket object. Assuming we created Socket object as
mentioned above:
Attribute
Socket.readyState
Description
The readonly attribute readyState represents the state of the
connection. It can have the following values:
1.
2.
3.
4.
Socket.bufferedAmount
WebSocket Events:
17 | P a g e
A value of 0 indicates that the connection has not yet
been established.
A value of 1 indicates that the connection is established
and communication is possible.
A value of 2 indicates that the connection is going through
the closing handshake.
A value of 3 indicates that the connection has been closed
or could not be opened.
The readonly attribute bufferedAmount represents the number
of bytes of UTF-8 text that have been queued using send()
method.