summaryrefslogtreecommitdiff
path: root/examples/websockets/sslechoserver/sslechoclient.html
blob: b9faa44afde8d3fa91b7e9f8fbc0eb3720fb0028 (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
<!DOCTYPE html>
<html>
    <head>
        <title>Websocket Echo Client</title>
        <meta charset="utf-8">
    </head>
    <body>
        <h1>Websocket Echo Client</h1>
        <p>
            <button onClick="initWebsocket();">Connect</button>
            <button onClick="stopWebsocket();">Disconnect</button>
            <button onClick="checkSocket();">State</button>
        </p>
        <p>
            <textarea id="debugTextArea" style="width:400px;height:200px;"></textarea>
        </p>
        <p>
            <input type="text" id="inputText" onkeydown="if(event.keyCode==13)sendMessage();"/>
            <button onClick="sendMessage();">Send</button>
        </p>

        <script type="text/javascript">
            var debugTextArea = document.getElementById("debugTextArea");
            var wsUri = "wss://localhost:1234";
            var websocket = null;

            function debug(message) {
                debugTextArea.value += message + "\n";
                debugTextArea.scrollTop = debugTextArea.scrollHeight;
            }

            function sendMessage() {
                var msg = document.getElementById("inputText").value;
                if ( websocket != null )
                {
                    document.getElementById("inputText").value = "";
                    websocket.send( msg );
                    console.log( "string sent :", '"'+msg+'"' );
                }
            }

            function initWebsocket() {
                try {
                    if (typeof MozWebSocket == 'function')
                        WebSocket = MozWebSocket;
                    if ( websocket && websocket.readyState == 1 )
                        websocket.close();
                    websocket = new WebSocket( wsUri );
                    websocket.onopen = function (evt) {
                        debug("CONNECTED");
                    };
                    websocket.onclose = function (evt) {
                        debug("DISCONNECTED");
                    };
                    websocket.onmessage = function (evt) {
                        console.log( "Message received :", evt.data );
                        debug( evt.data );
                    };
                    websocket.onerror = function (evt) {
                        debug('ERROR: ' + evt.data);
                    };
                } catch (exception) {
                    debug('ERROR: ' + exception);
                }
            }

            function stopWebsocket() {
                if (websocket)
                    websocket.close();
            }

            function checkSocket() {
                if (websocket != null) {
                    var stateStr;
                    switch (websocket.readyState) {
                        case 0: {
                            stateStr = "CONNECTING";
                            break;
                        }
                        case 1: {
                            stateStr = "OPEN";
                            break;
                        }
                        case 2: {
                            stateStr = "CLOSING";
                            break;
                        }
                        case 3: {
                            stateStr = "CLOSED";
                            break;
                        }
                        default: {
                            stateStr = "UNKNOW";
                            break;
                        }
                    }
                    debug("Websocket state = " + websocket.readyState + " ( " + stateStr + " )");
                } else {
                    debug("Websocket is null");
                }
            }
        </script>
    </body>
</html>