HTML and CSS Reference
In-Depth Information
Listing 6-20. Creating a WebSocket
<script type="text/javascript">
function myWS() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
var ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function() {
ws.send("Message to send");
alert("Message sent...");
ws.onmessage = function (evt) {
var received_msg =;
alert("Message received...");
ws.onclose = function() {
alert("Connection closed...");
else {
alert("WebSocket is not supported by your browser!");
In the document body, the myWS() function should be called to start the WebSocket (Listing 6-21).
Listing 6-21. An Anchor to Start the WebSocket
<a href="javascript:myWS()">Start WebSocket</a>
The client program is now ready, but we also need a server with WebSocket support to test it. For example,
pywebsocket, which can be used as a WebSocket stand-alone server and a WebSocket extension for Apache HTTP
servers, is suitable for testing [16].
After the HTTP handshake, the TCP socket is ready for use, and the connection is live; both the server and the
client can send data.
On the client side, the WebSocket API is supported by Firefox 4+, Google Chrome 4+, Safari 5+, and Opera 11+.
Offline Web Applications
The offline web application feature in HTML5 allows online applications to work without interruption even when the
Internet connection is not available. For example, users can compose a message in their webmail client when they
cannot find a Wi-Fi hotspot.
Since the browser has no access to web site files when it is offline, the first step is to specify the required resources
(a simple list of fundamental files) for caching in a file called offline.manifest (Listing 6-22).
Search WWH ::

Custom Search