summaryrefslogtreecommitdiff
path: root/tests/simple.html
blob: 8f6a73fb02dda2314e71adac2a78fd89f2e4fb39 (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
<html>

    <head>
        <title>Websock Simple Client</title>
    </head>

    <body>
        WebSocket/websockify URI: <input id='target'>&nbsp;
        <input id='connectButton' type='button' value='Connect'
            onclick="connect();">
        <br> <br>
        <input id='sendText'>&nbsp;
        <input id='sendButton' type='button' value='Send' disabled
            onclick="send();">&nbsp;
        <br> <br>
        Log:<br><textarea id="messages" cols=80 rows=25></textarea>
    </body>


    <script>
        var document.getElementById = function(id) { return document.getElementById(id); },
            ws = null, msgs = document.getElementById('messages');

        function msg(str) {
            msgs.innerHTML += str + "\n";
            msgs.scrollTop = msgs.scrollHeight;
        }

        function connect() {
            var uri = document.getElementById('target').value;
            msg("connecting to: " + uri);
            ws = new WebSocket(uri);
            ws.binaryType = 'arraybuffer';
            ws.addEventListener('open', function () {
                msg("Connected");
            });
            ws.addEventListener('message', function (e) {
                msg("Received: " + e.data);
            });
            ws.addEventListener('close', function () {
                disconnect();
                msg("Disconnected");
            });

            document.getElementById('connectButton').value = "Disconnect";
            document.getElementById('connectButton').onclick = disconnect;
            document.getElementById('sendButton').disabled = false;
        }

        function disconnect() {
            if (ws) { ws.close(); }
            ws = null;

            document.getElementById('connectButton').value = "Connect";
            document.getElementById('connectButton').onclick = connect;
            document.getElementById('sendButton').disabled = true;
        }

        function send() {
            msg("Sending: " + document.getElementById('sendText').value);
            ws.send_string(document.getElementById('sendText').value);
        };
    </script>

</html>