aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/js/chat.js
blob: 1ea3471f7c4cd2a41afc4c1ae2eaca043d02f05a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 + "<br>";
};

$('#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("");
}