-
Notifications
You must be signed in to change notification settings - Fork 43
/
chat_client.html
57 lines (53 loc) · 1.73 KB
/
chat_client.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebSocket Chat - CppCon2018</title>
</head>
<body>
<h1>WebSocket Chat</h1>
<p>Source code: <a href="https://github.com/vinniefalco/CppCon2018">https://github.com/vinniefalco/CppCon2018</a></p>
Server URI: <input class="draw-border" id="uri" size="47" value="ws://localhost:8080" style="margin-bottom: 5px;">
<button class="echo-button" id="connect">Connect</button>
<button class="echo-button" id="disconnect">Disconnect</button><br>
Your Name: <input class="draw-border" id="userName" size=47 style="margin-bottom: 5px;"><br>
<pre id="messages" style="width: 600px; height: 400px; border: solid 1px #cccccc; margin-bottom: 5px;"></pre>
<div style="margin-bottom: 5px;">
Message<br>
<input class="draw-border" id="sendMessage" size="74" value="">
<button class="echo-button" id="send">Send</button>
</div>
<script>
var ws = null;
connect.onclick = function() {
ws = new WebSocket(uri.value);
ws.onopen = function(ev) {
messages.innerText += "[connection opened]\n";
};
ws.onclose = function(ev) {
messages.innerText += "[connection closed]\n";
};
ws.onmessage = function(ev) {
messages.innerText += ev.data + "\n";
};
ws.onerror = function(ev) {
messages.innerText += "[error]\n";
console.log(ev);
};
};
disconnect.onclick = function() {
ws.close();
};
send.onclick = function() {
ws.send(userName.value + ": " + sendMessage.value);
sendMessage.value = "";
};
sendMessage.onkeyup = function(ev) {
ev.preventDefault();
if (event.keyCode === 13) {
send.click();
}
}
</script>
</body>
</html>