-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_client.html
75 lines (71 loc) · 2.04 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebSocket Chat - CppCon2018</title>
</head>
<body>
<h1>WebSocket Chat</h1>
Server URI: <input class="draw-border" id="uri" size="47" value="ws://35.237.102.20:80" 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();
}
};
sendMessage.onkeydown = function(ev) {
ev.preventDefault();
str = ""
if (event.keyCode === 37) {
str += " left"
}
if (event.keyCode === 38) {
str += " up"
}
if (event.keyCode === 39) {
str += " right"
}
if (event.keyCode === 40) {
str += " down"
}
if (str != "") {
ws.send(userName.value + " moved " + str)
}
}
</script>
</body>
</html>