~ubuntu-branches/ubuntu/trusty/websockify/trusty-updates

« back to all changes in this revision

Viewing changes to tests/echo.html

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-02-23 01:22:51 UTC
  • Revision ID: package-import@ubuntu.com-20130223012251-3qkk1n1p93kb3j87
Tags: upstream-0.3.0+dfsg1
ImportĀ upstreamĀ versionĀ 0.3.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
 
 
3
    <head>
 
4
        <title>WebSockets Echo Test</title>
 
5
        <script src="include/util.js"></script>
 
6
        <script src="include/webutil.js"></script> 
 
7
        <script src="include/base64.js"></script>
 
8
        <script src="include/websock.js"></script> 
 
9
        <!-- Uncomment to activate firebug lite -->
 
10
        <!--
 
11
        <script type='text/javascript' 
 
12
            src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
 
13
        -->
 
14
 
 
15
 
 
16
    </head>
 
17
 
 
18
    <body>
 
19
 
 
20
        Host: <input id='host' style='width:100'>&nbsp;
 
21
        Port: <input id='port' style='width:50'>&nbsp;
 
22
        Encrypt: <input id='encrypt' type='checkbox'>&nbsp;
 
23
        <input id='connectButton' type='button' value='Start' style='width:100px'
 
24
            onclick="connect();">&nbsp;
 
25
 
 
26
 
 
27
        <br>
 
28
        Log:<br>
 
29
        <textarea id="messages" style="font-size: 9;" cols=80 rows=25></textarea>
 
30
    </body>
 
31
 
 
32
 
 
33
    <script>
 
34
        var ws, host = null, port = null,
 
35
            msg_cnt = 0, send_cnt = 1, echoDelay = 500,
 
36
            echo_ref;
 
37
 
 
38
        function message(str) {
 
39
            console.log(str);
 
40
            cell = $D('messages');
 
41
            cell.innerHTML += msg_cnt + ": " + str + "\n";
 
42
            cell.scrollTop = cell.scrollHeight;
 
43
            msg_cnt++;
 
44
        }
 
45
 
 
46
        Array.prototype.pushStr = function (str) {
 
47
            var n = str.length;
 
48
            for (var i=0; i < n; i++) {
 
49
                this.push(str.charCodeAt(i));
 
50
            }
 
51
        }
 
52
 
 
53
        function send_msg() {
 
54
            var str = "Message #" + send_cnt;
 
55
            ws.send_string(str);
 
56
            message("Sent message: '" + str + "'");
 
57
            send_cnt++;
 
58
        }
 
59
 
 
60
        function update_stats() {
 
61
            $D('sent').innerHTML = sent;
 
62
            $D('received').innerHTML = received;
 
63
            $D('errors').innerHTML = errors;
 
64
        }
 
65
 
 
66
        function connect() {
 
67
            var host = $D('host').value,
 
68
                port = $D('port').value,
 
69
                scheme = "ws://", uri;
 
70
 
 
71
            console.log(">> connect");
 
72
            if ((!host) || (!port)) {
 
73
                console.log("must set host and port");
 
74
                return;
 
75
            }
 
76
 
 
77
            if (ws) {
 
78
                ws.close();
 
79
            }
 
80
 
 
81
            if ($D('encrypt').checked) {
 
82
                scheme = "wss://";
 
83
            }
 
84
            uri = scheme + host + ":" + port;
 
85
            message("connecting to " + uri);
 
86
 
 
87
            ws = new Websock();
 
88
            ws.open(uri);
 
89
 
 
90
            ws.on('message', function(e) {
 
91
                //console.log(">> WebSockets.onmessage");
 
92
                var str = ws.rQshiftStr();
 
93
 
 
94
                message("Received message '" + str + "'");
 
95
                //console.log("<< WebSockets.onmessage");
 
96
            });
 
97
            ws.on('open', function(e) {
 
98
                console.log(">> WebSockets.onopen");
 
99
                echo_ref = setInterval(send_msg, echoDelay);
 
100
                console.log("<< WebSockets.onopen");
 
101
            });
 
102
            ws.on('close', function(e) {
 
103
                console.log(">> WebSockets.onclose");
 
104
                if (echo_ref) {
 
105
                    clearInterval(echo_ref);
 
106
                    echo_ref = null;
 
107
                }
 
108
                console.log("<< WebSockets.onclose");
 
109
            });
 
110
            ws.on('error', function(e) {
 
111
                console.log(">> WebSockets.onerror");
 
112
                if (echo_ref) {
 
113
                    clearInterval(echo_ref);
 
114
                    echo_ref = null;
 
115
                }
 
116
                console.log("<< WebSockets.onerror");
 
117
            });
 
118
 
 
119
            $D('connectButton').value = "Stop";
 
120
            $D('connectButton').onclick = disconnect;
 
121
            console.log("<< connect");
 
122
        }
 
123
 
 
124
        function disconnect() {
 
125
            console.log(">> disconnect");
 
126
            if (ws) {
 
127
                ws.close();
 
128
            }
 
129
 
 
130
            if (echo_ref) {
 
131
                clearInterval(echo_ref);
 
132
            }
 
133
 
 
134
            $D('connectButton').value = "Start";
 
135
            $D('connectButton').onclick = connect;
 
136
            console.log("<< disconnect");
 
137
        }
 
138
 
 
139
 
 
140
        window.onload = function() {
 
141
            console.log("onload");
 
142
            var url = document.location.href;
 
143
            $D('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
 
144
            $D('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
 
145
        }
 
146
    </script>
 
147
 
 
148
</html>