var wsUri; var output; var count; var ws; window.addEventListener("load", function(evt) { wsUri = "ws://" + window.location.host + "/stream/stream" output = document.getElementById("output"); count = document.getElementById("count"); var print = function(message) { var d = document.createElement("div"); d.innerHTML = message; output.appendChild(d); }; var parseCount = function(evt) { return JSON.parse(evt.data).count }; var newSocket = function() { ws = new WebSocket(wsUri); ws.onopen = function(evt) { print('Connection Open'); } ws.onclose = function(evt) { print('Connection Closed'); ws = null; } ws.onmessage = function(evt) { print('Update: ' + parseCount(evt)); } ws.onerror = function(evt) { print('Error: ' + parseCount(evt)); } }; newSocket() document.getElementById("send").onclick = function(evt) { if (!ws) { return false } var msg = { count: parseInt(count.value) } req = JSON.stringify(msg) print('Sent request: ' + req); ws.send(JSON.stringify(msg)); return false; }; document.getElementById("cancel").onclick = function(evt) { if (!ws) { return false; } ws.close(); print('Request Canceled'); return false; }; document.getElementById("open").onclick = function(evt) { if (!ws) { newSocket() } return false; }; })