summaryrefslogtreecommitdiff
path: root/tests/plain_echo.html
blob: be35e54389bbf878431e1aefc1cc4bc09534f9fb (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
139
140
141
142
143
144
145
<html>

    <head>
        <title>WebSockets Echo Test</title>
    </head>

    <body>

        Host: <input id='host' style='width:100'>&nbsp;
        Port: <input id='port' style='width:50'>&nbsp;
        Encrypt: <input id='encrypt' type='checkbox'>&nbsp;
        <input id='connectButton' type='button' value='Start' style='width:100px'
            onclick="connect();">&nbsp;


        <br>
        Log:<br>
        <textarea id="messages" style="font-size: 9;" cols=80 rows=25></textarea>
    </body>


    <script>
        var ws, host = null, port = null,
            msg_cnt = 0, send_cnt = 1, echoDelay = 500,
            echo_ref;

        function message(str) {
            console.log(str);
            cell = document.getElementById('messages');
            cell.innerHTML += msg_cnt + ": " + str + "\n";
            cell.scrollTop = cell.scrollHeight;
            msg_cnt++;
        }

        Array.prototype.pushStr = function (str) {
            var n = str.length;
            for (var i=0; i < n; i++) {
                this.push(str.charCodeAt(i));
            }
        }

        function send_msg() {
            if (ws.bufferedAmount > 0) {
                console.log("Delaying send");
                return;
            }
            var str = "Message #" + send_cnt, arr = [];
            var encoder = new TextEncoder();
            ws.send(encoder.encode(str));
            message("Sent message: '" + str + "'");
            send_cnt++;
        }

        function update_stats() {
            document.getElementById('sent').innerHTML = sent;
            document.getElementById('received').innerHTML = received;
            document.getElementById('errors').innerHTML = errors;
        }

        function init_ws() {
            console.log(">> init_ws");
            console.log("<< init_ws");
        }

        function connect() {
            var host = document.getElementById('host').value,
                port = document.getElementById('port').value,
                scheme = "ws://", uri;

            console.log(">> connect");
            if ((!host) || (!port)) {
                console.log("must set host and port");
                return;
            }

            if (ws) {
                ws.close();
            }

            if (document.getElementById('encrypt').checked) {
                scheme = "wss://";
            }
            uri = scheme + host + ":" + port;
            message("connecting to " + uri);
            ws = new WebSocket(uri);
            ws.binaryType = 'arraybuffer';

            ws.addEventListener('message', function(e) {
                //console.log(">> WebSockets.onmessage");
                var decoder = new TextDecoder('UTF-8');
                var str = decoder.decode(e.data);
                message("Received message '" + str + "'");
                //console.log("<< WebSockets.onmessage");
            });
            ws.addEventListener('open', function(e) {
                console.log(">> WebSockets.onopen");
                echo_ref = setInterval(send_msg, echoDelay);
                console.log("<< WebSockets.onopen");
            });
            ws.addEventListener('close', function(e) {
                console.log(">> WebSockets.onclose");
                if (echo_ref) {
                    clearInterval(echo_ref);
                    echo_ref = null;
                }
                console.log("<< WebSockets.onclose");
            });
            ws.addEventListener('error', function(e) {
                console.log(">> WebSockets.onerror");
                if (echo_ref) {
                    clearInterval(echo_ref);
                    echo_ref = null;
                }
                console.log("<< WebSockets.onerror");
            });

            document.getElementById('connectButton').value = "Stop";
            document.getElementById('connectButton').onclick = disconnect;
            console.log("<< connect");
        }

        function disconnect() {
            console.log(">> disconnect");
            if (ws) {
                ws.close();
            }

            if (echo_ref) {
                clearInterval(echo_ref);
            }

            document.getElementById('connectButton').value = "Start";
            document.getElementById('connectButton').onclick = connect;
            console.log("<< disconnect");
        }

        window.onload = function() {
            console.log("onload");
            var url = document.location.href;
            document.getElementById('host').value = (url.match(/host=([^&#]*)/) || ['',window.location.hostname])[1];
            document.getElementById('port').value = (url.match(/port=([^&#]*)/) || ['',window.location.port])[1];
        }
    </script>

</html>