From 69af3f60d399157a8951988fa2f91d29c553fbb5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 25 Jul 2016 21:36:50 +0300 Subject: juick-ws: websocket terminal --- juick-ws/src/main/static/scripts.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 juick-ws/src/main/static/scripts.js (limited to 'juick-ws/src/main/static/scripts.js') diff --git a/juick-ws/src/main/static/scripts.js b/juick-ws/src/main/static/scripts.js new file mode 100644 index 00000000..2691816d --- /dev/null +++ b/juick-ws/src/main/static/scripts.js @@ -0,0 +1,36 @@ +var Terminal = require('terminal'); +require('whatwg-fetch'); + +function ready(fn) { + if (document.readyState != 'loading') { + fn(); + } else { + document.addEventListener('DOMContentLoaded', fn); + } +} + +ready(function() { + var ws = new WebSocket('wss://ws.juick.com/_replies'); + var term = new Terminal('terminal', {}, {}); + var status = document.querySelector("#status"); + ws.onopen = function() { + term.output("
connected"); + }; + ws.onclose = function() { + term.output("
disconnected"); + } + ws.onmessage = function(msg) { + term.output("
" + JSON.stringify(JSON.parse(msg.data), null, 2)); + } + setTimeout(function() { + ws.send(" "); + }, 60000); + setTimeout(function() { + fetch("/api/status") + .then(function(response) { + return response.text(); + }).then(function(body) { + status.textContent = JSON.parse(body).clientsCount; + }) + }, 5000); +}) \ No newline at end of file -- cgit v1.2.3