summaryrefslogtreecommitdiff
path: root/static/test/bugchat.js
blob: 5d9a7b52999e56b3d80a457865a3f3425537d68b (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function generateRandomKey(length) {
    s = "";
    for (var i = 0; i < length; i++) {
        var r = Math.floor(Math.random() * 26);
        s += alphaChars[r];
    }
    return s;
}

function formatTime() {
    var a_p = "";
    var d = new Date();
    var curr_hour = d.getHours();
    if (curr_hour < 12)
    {
        a_p = "AM";
    }
    else
    {
        a_p = "PM";
    }
    if (curr_hour == 0)
    {
        curr_hour = 12;
    }
    if (curr_hour > 12)
    {
        curr_hour = curr_hour - 12;
    }

    if (curr_hour.length == 1)
        curr_hour = "0" + curr_hour;

    var curr_min = d.getMinutes();

    curr_min = curr_min + "";

    if (curr_min.length == 1)
    {
        curr_min = "0" + curr_min;
    }
    return curr_hour + ":" + curr_min + " " + a_p;
}

Key = generateRandomKey(10);
Timestamp = 0;
Counter = 0;
ClientMap = {};

function showError(err) {
    var msg = formatTime() + "  " + err;
    console.error(msg);
    $('#msgs').append($('<div>').css('color', 'red').text(msg));
};


function handleMessages(msgs) {
    $.map(msgs, function(m) {
        var split = m.content.split("-");
        if (split.length != 2) {
            showError("Bad message " + m.content);
            return;
        }
        var client = split[0];
        var i = parseInt(split[1]);
        console.warn(client + " " + i);
        
        if (client in ClientMap) {
            var p = ClientMap[client];
            if (i - p != 1) {
                showError("[" + Key + "] Error for client " + client
                          + "! Expected " + (p+1) + ", got " + i);
            }
        }
        ClientMap[client] = i;
    });
}

function refresh() {
    var onSuccess = function(json) {
        try {
            Timestamp = json.timestamp;
            handleMessages(json.messages);
        } catch(e) {
            console.error(e);
            $('#msgs').append($('<div>').css('color', 'red').text(e));
        }
        setTimeout(refresh, 1000);
    };
    var onError = function(resp, textStatus, errorThrown) {
        console.error($.trim(resp.responseText));
        setTimeout(refresh, 1000);
    };

    $.ajax({
        type: 'GET',
        timeout: 5000,
        url: '/refresh',
        data: { 'room': 'test', 'since': Timestamp },
        cache: false,
        dataType: 'json',
        success: onSuccess,
        error: onError
    });
}

function postMessageWrapper() {
    var delay = Math.ceil(1000 + Math.random() * 3000);
    setTimeout(postMessage, delay);
}

function postMessage() {
    var content = Key + "-" + Counter;
    Counter++;
    var onError = function(resp, textStatus, errorThrown) {
        showError("Error posting " + content + ": " + resp.responseText);
        postMessageWrapper();
    };

    $.ajax({
        type: 'POST',
        timeout: 5000,
        url: '/msg',
        data: { 'room': 'test', 'content': content },
        cache: false,
        dataType: 'json',
        success: postMessageWrapper,
        error: onError
    });
}

function initClient() {
    showError("Initializing client " + Key);
    refresh();
    postMessageWrapper();
};