File size: 3,034 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<meta charset="utf-8" />
<html>
<head>
  <script language="javascript" type="text/javascript">
    var socket = null;

    function log(msg) {
        const logElem = document.getElementById("log");
        const p = document.createElement("p");
        p.textContent = msg;
        logElem.appendChild(p);
        logElem.scroll(0, logElem.scrollHeight);
    }

    function connect() {
        disconnect();
        socket = new WebSocket(document.getElementById("wsUri").value);
        log("Connecting...");
        socket.addEventListener("open", function() {
            log("Connected.");
            update_ui();
        });
        socket.addEventListener("message", function(e) {
            log("Received: " + e.data);
        });
        socket.addEventListener("close", function() {
            log("Disconnected.");
            socket = null;
            update_ui();
        });
    }

    function disconnect() {
        if (socket !== null) {
            log("Disconnecting...");
            socket.close();
            socket = null;
            update_ui();
        }
    }

    function update_ui() {
        const status = document.getElementById("status");
        const connect = document.getElementById("connect");
        if (socket === null) {
            status.innerText = "disconnected";
            connect.innerText = "Connect";
        } else {
            status.innerText = "connected (" + socket.protocol + ")";
            connect.innerText = "Disconnect";
        }
    }

    window.addEventListener("DOMContentLoaded", function() {
        const protocol = (window.location.protocol=="https:" && "wss://" || "ws://");
        document.getElementById("wsUri").value = protocol + (window.location.host || "localhost:8080");

        document.getElementById("connect").addEventListener("click", function() {
            if (socket == null) {
                connect();
            } else {
                disconnect();
            }
            update_ui();
            return false;
        });

        document.getElementById("send").addEventListener("click", function() {
            const text = document.getElementById("text");
            log("Sending: " + text.value);
            socket.send(text.value);
            text.value = "";
            text.focus();
            return false;
        });

        document.getElementById("text").addEventListener("keyup", function(e) {
            if (e.keyCode === 13) {
                document.getElementById("send").click();
                return false;
            }
        });
    });
</script>
</head>
<body>
<h3>Chat!</h3>
<div>
  <input id="wsUri" type="text" />
  <button id="connect">Connect</button>&nbsp;|&nbsp;Status:
  <span id="status">disconnected</span>
</div>
<div id="log"
     style="width:20em;height:15em;overflow:auto;border:1px solid black">
</div>
<form id="chatform" onsubmit="return false;">
  <input id="text" type="text" />
  <input id="send" type="button" value="Send" />
</form>
</body>
</html>