summaryrefslogtreecommitdiffstats
path: root/webapp/app/templates/index.html
blob: d4d1853234a0513d32605ce7bda61460105fdd45 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<html>
    <head>
        <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
    
        <script>
            var SPAWN_URL = "/spawn";

            function creature(name, socket) {
                var self = this;
                self.name = name;
                self.socket = socket;
                
                self.render = function($parent) {
                    var $a = $('<a>');
                    $a.text(self.name);
                    $a.click(self.click.bind(self));

                    var $li = $('<li>');
                    $li.append($a);
                    $parent.append($li);
                };

                self.click = function() {
                    self.socket.emit('spawn', {name:self.name});
                }
            }

            $(document).ready(function() {
                var namespace = '/spawn';
                var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
                
                socket.on('connect', function() {
                    console.log('connected!');
                });

                socket.on('new creature', function(msg) {
                    console.log('new!');
                    var c = new creature(msg.name, socket);
                    c.render($('#creatures'));
                });

                socket.on('disconnect', function() {
                    console.log('disconnected');
                });

                socket.emit('ready');
            });

        </script>    
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>Ocean Fun Times</h1>
        <ul id="creatures"></ul>
    </body>
</html>