HTML5 Course. HTML5 Certification, HTML5 Training, HTML5 Tutorials.
Immediately you get a web socket connection to the web server, you'll have the access to send data from the web browser to the server just by calling the send() and also accept incoming data from the server to the web browser by an onmessage event handler.
The code below is the API that initiates a new WebSocket object as specified by HTML5
var Socket = new WebSocket(url, [protocol] );
Here first parameter, URL, specify the URL to which to connect. The second parameter, protocol is mainly optional and not always necessary, and if present specifies a sub-protocol that the server must support for the connection to be successful.
The following are the attribute that is available for the WebSocket object.
|Sr.No.||Attribute & Description|
The read-only attribute readyState stands for the current state of the connection. It can have the following values:
The bufferedAmount stands for the value/number of bytes of UTF-8 text that have been queued using the send() function and it is read-only.
The following are the events that are available for the WebSocket object.
|open||Socket.onopen||This event is initiated when the socket connection is established.|
|message||Socket.onmessage||This event is initiated when the client receives data from the server.|
|error||Socket.onerror||This event is initiated when there is an error in communication.|
|close||Socket.onclose||This event is initiated when the connection is closed.|
The following are the methods that are available to work with the WebSocket object.
|Sr.No.||Method & Description|
The send(data) method sends data using the connection.
The close() method is used to terminate an existing connection.
A WebSocket is a two-direction TCP socket that occurs between the client and the server. The TCP socket initiates as an HTTP connection and is then upgraded to a TCP socket after an HTTP handshake. After the HTTP handshake, both the client and the server can transmit data.
There are currently only a few web browsers that support the WebSocket() interface. You can test out the code below with the newest version of Chrome, Mozilla, Opera, and Safari.
Just before you test the code above, you need to install a server that has support for WebSocket. You have to download mod_pywebsocket-x.x.x.tar.gz from pywebsocket which main aim is to create a Web Socket extension for the Apache HTTP server and it can be installed using the following steps:
1. Unzip and untar the file you downloaded.
2. Go into pywebsocket-x.x.x/src/ directory.
3. Type $python setup.py build into your terminal
4. Type $sudo python setup.py install into your terminal
5. Then open the document by typing:
This will install the package into your python environment.
Navigate to the pywebsocket-x.x.x/src/mod_pywebsocket directory and run the following:
$sudo python standalone.py -p 9998 -w ../example/
This will kickstart the server and it will be listening at port 9998 and use the handlers directory listed by the -w option where our echo_wsh.py is.
Now making use of the Chrome browser, open the HTML file we created from the beginning. If your browser has support for WebSocket(), then an alert will be displayed indicating that your web browser has support for WebSocket and then you can click on the "Run WebSocket" and you'll receive Goodbye message sent by the server script.
Login & Study At Your Pace
500+ Relevant Tech Courses
300,000+ Enrolled Students
Don't have an account? Create your account to Start!
The Scholarship offer is a discount program to take our Course Programs and Certification valued at $70 USD for a reduced fee of $7 USD. - Offer Closes Soon!!
Don't have an account? Create your account to Start Learning!