Book Image

Websocket Essentials: Building apps with HTML5 websockets

By : Varun Chopra
Book Image

Websocket Essentials: Building apps with HTML5 websockets

By: Varun Chopra

Overview of this book

Table of Contents (13 chapters)
WebSocket Essentials – Building Apps with HTML5 WebSockets
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
6
Enhancing HTML5 Web Application Development Using Modern Tools
Index

The WebSockets client app


Let us start with writing client-side code in JavaScript. We will be hitting the same Echo server for now. Let's get started with our client-side code. Here is how the client code will look:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >
    <title>WebSocket Test</title>
    <script language="javascript" type="text/javascript">
      var wsUri = "ws://echo.websocket.org/";
      var output;

      function init(){
          output = document.getElementById("output");
          testWebSocket();
      }
      function testWebSocket(){

          websocket = new WebSocket(wsUri);

          websocket.onopen = onOpen;

          websocket.onclose = onClose;

          websocket.onmessage = onMessage;

          websocket.onerror = onError;

      }

      function onOpen(evt){
          writeToScreen("CONNECTED");
          doSend("WebSocket rocks");
      }

      function onClose(evt){
          writeToScreen(...