From a935f6bc393490d02119e9c6cbc62787b3656fac Mon Sep 17 00:00:00 2001 From: marvin-borner@live.com Date: Wed, 11 Apr 2018 17:02:06 +0200 Subject: Added JSON as transport type - begin JS encryption --- assets/js/chat.js | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'assets/js/chat.js') diff --git a/assets/js/chat.js b/assets/js/chat.js index 1ea3471..9543f87 100644 --- a/assets/js/chat.js +++ b/assets/js/chat.js @@ -1,33 +1,49 @@ -var conn = new WebSocket('wss://marvinborner.ddnss.de:1337'); -conn.onopen = function () { - console.log("Chat connection established!"); -}; +var ChatTextInput = $("#ChatTextInput"); +var SubscribeTextInput = $("#SubscribeTextInput"); +var ChatResponses = $("#ChatResponses"); -conn.onmessage = function (e) { - document.getElementById("ChatResponses").innerHTML += e.data + "
"; +var WebSocket = new WebSocket('wss://marvinborner.ddnss.de:1337'); +WebSocket.onopen = function () { + //console.log("Chat connection established!"); +}; +WebSocket.onmessage = function (e) { + var MessageObject = JSON.parse(e.data); + if (MessageObject.ServerMessage === false) { + ChatResponses.append(MessageObject.Username + " - " + MessageObject.Message + "
"); + } else if (MessageObject.ServerMessage === true) { + if (MessageObject.ServerMessageType === "GroupJoin") { + if (MessageObject.WasHimself === false) { + ChatResponses.append(MessageObject.Username + " joined the group.
"); + } else if (MessageObject.WasHimself === true) { + ChatResponses.empty(); + ChatResponses.append("You joined the group " + MessageObject.GroupName + ".
"); + } + } else if (MessageObject.ServerMessageType === "UserDisconnect") { + ChatResponses.append(MessageObject.Username + " disconnected from the Server."); + } + } }; -$('#ChatTextInput').keyup(function (e) { +ChatTextInput.keyup(function (e) { if (e.keyCode === 13) { - sendMessage($('#ChatTextInput').val()); - $('#ChatTextInput').val(""); + sendMessage(ChatTextInput.val()); + ChatTextInput.val(""); } }); -$('#SubscribeTextInput').keyup(function (e) { +SubscribeTextInput.keyup(function (e) { if (e.keyCode === 13) { - subscribe($('#SubscribeTextInput').val()); + subscribe(SubscribeTextInput.val()); } }); function subscribe(channel) { - conn.send(JSON.stringify({command: "subscribe", channel: channel})); - $("#SubscribeTextInput").hide(); - $("#ChatTextInput").show(); - $("#ChatResponses").empty(); + WebSocket.send(JSON.stringify({ClientMessageType: "Subscribe", Channel: channel})); + SubscribeTextInput.hide(); + ChatTextInput.show(); } function sendMessage(msg) { - conn.send(JSON.stringify({command: "message", message: msg})); - $("#ChatTextInput").val(""); + WebSocket.send(JSON.stringify({ClientMessageType: "Message", Message: msg})); + ChatTextInput.val(""); } -- cgit v1.2.3