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 ...
Get WebSocket Essentials – Building Apps with HTML5 WebSockets now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.