summaryrefslogtreecommitdiff
path: root/public/js/chat.js
blob: 27b0c8e6cb67d1fa6c1093721121347a6dcf668e (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
/* -*- mode:javascript; coding:utf-8; -*- */

'use strict';

(function (window) {
    function on_ended(context) {
        if (this.link && this.link.buffer) {
            console.log('playing next sound');
            queue.link = this.link;
            this.link.onended = _.partial(on_ended, context);
            this.link.play(context);
        }
        else {
            console.log('playing again');
            this.play(context);
        }
    };

    function Queue() {
        this.last = this;
    }

    Queue.prototype.append = function (x) {
        if (!this.seen || x.gt(this.seen)) {
            this.last.link = x;
            this.last = x;
            this.seen = {
                seq: x.seq,
                timestamp: x.timestamp
            };
            return x;
        }
        else {
            return null;
        }
    };

    var queue = new Queue();

    window.AudioContext = window.AudioContext || window.webkitAudioContext;

    window.onload = function () {
        var context = new AudioContext();
        var field = document.getElementById("field");
        var history = document.getElementById("history");
        var send = document.getElementById('send');

        var messages = [];
        var socket = io.connect('/');

        socket.on('message', function (data) {
            if (data.message) {
                messages.push(data.message);

                var html = '';

                for (var i = 0; i < messages.length; i++) {
                    html += messages[i] + '<br />';
                }
                history.innerHTML = html;

                if (data.queue) {
                    _.each(data.queue, function (s) {
                        try {
                            var sample = new Sample(s);

                            if (queue.append(sample)) {
                                if (queue.link === sample) {
                                    sample.onended = _.partial(on_ended, context);
                                    sample.onload = function () {
                                        this.play(context);
                                    };
                                }
                                sample.load(context);
                            }
                        }
                        catch (e) {
                            console.error((e.message || 'error') + ' in "' + s + '"');
                        }
                    });
                }
            }
            else {
                console.error('error: ', data);
            }
        });

        send.onclick = function () {
            var text = field.value;
            socket.emit('send', { message: text });
        };
    };
})(window);