O'Reilly logo

WebSocket Essentials – Building Apps with HTML5 WebSockets by Varun Chopra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required