aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/js/chat.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/js/chat.js')
-rw-r--r--assets/js/chat.js50
1 files changed, 33 insertions, 17 deletions
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 + "<br>";
+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 + "<br>");
+ } else if (MessageObject.ServerMessage === true) {
+ if (MessageObject.ServerMessageType === "GroupJoin") {
+ if (MessageObject.WasHimself === false) {
+ ChatResponses.append(MessageObject.Username + " joined the group. <br>");
+ } else if (MessageObject.WasHimself === true) {
+ ChatResponses.empty();
+ ChatResponses.append("You joined the group " + MessageObject.GroupName + ".<br>");
+ }
+ } 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("");
}