1<html> 2<head> 3 <title>Simple client</title> 4 5 <script type="text/javascript"> 6 7 var ws; 8 9 function init() { 10 11 // Connect to Web Socket 12 ws = new WebSocket("ws://localhost:9001/"); 13 14 // Set event handlers. 15 ws.onopen = function() { 16 output("onopen"); 17 }; 18 19 ws.onmessage = function(e) { 20 // e.data contains received string. 21 output("onmessage: " + e.data); 22 }; 23 24 ws.onclose = function() { 25 output("onclose"); 26 }; 27 28 ws.onerror = function(e) { 29 output("onerror"); 30 console.log(e) 31 }; 32 33 } 34 35 function onSubmit() { 36 var input = document.getElementById("input"); 37 // You can send message to the Web Socket using ws.send. 38 ws.send(input.value); 39 output("send: " + input.value); 40 input.value = ""; 41 input.focus(); 42 } 43 44 function onCloseClick() { 45 ws.close(); 46 } 47 48 function output(str) { 49 var log = document.getElementById("log"); 50 var escaped = str.replace(/&/, "&").replace(/</, "<"). 51 replace(/>/, ">").replace(/"/, """); // " 52 log.innerHTML = escaped + "<br>" + log.innerHTML; 53 } 54 55 </script> 56</head> 57<body onload="init();"> 58 <form onsubmit="onSubmit(); return false;"> 59 <input type="text" id="input"> 60 <input type="submit" value="Send"> 61 <button onclick="onCloseClick(); return false;">close</button> 62 </form> 63 <div id="log"></div> 64</body> 65</html> 66