From fc9401f04a3aca5abb22f87ebc210de8afe11d32 Mon Sep 17 00:00:00 2001 From: marvin-borner@live.com Date: Tue, 10 Apr 2018 21:50:16 +0200 Subject: Initial Commit --- assets/js/chat.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 assets/js/chat.js (limited to 'assets/js/chat.js') diff --git a/assets/js/chat.js b/assets/js/chat.js new file mode 100644 index 0000000..1ea3471 --- /dev/null +++ b/assets/js/chat.js @@ -0,0 +1,33 @@ +var conn = new WebSocket('wss://marvinborner.ddnss.de:1337'); +conn.onopen = function () { + console.log("Chat connection established!"); +}; + +conn.onmessage = function (e) { + document.getElementById("ChatResponses").innerHTML += e.data + "
"; +}; + +$('#ChatTextInput').keyup(function (e) { + if (e.keyCode === 13) { + sendMessage($('#ChatTextInput').val()); + $('#ChatTextInput').val(""); + } +}); + +$('#SubscribeTextInput').keyup(function (e) { + if (e.keyCode === 13) { + subscribe($('#SubscribeTextInput').val()); + } +}); + +function subscribe(channel) { + conn.send(JSON.stringify({command: "subscribe", channel: channel})); + $("#SubscribeTextInput").hide(); + $("#ChatTextInput").show(); + $("#ChatResponses").empty(); +} + +function sendMessage(msg) { + conn.send(JSON.stringify({command: "message", message: msg})); + $("#ChatTextInput").val(""); +} -- cgit v1.2.3