-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebchat.html
More file actions
88 lines (74 loc) · 1.92 KB
/
webchat.html
File metadata and controls
88 lines (74 loc) · 1.92 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Web Chat</title>
<style>
body, input, select, textarea {
background: #031e11;
color: #10fd60;
}
#messages {
width: 80em;
height: 40em;
border: solid 1px #cccccc;
margin-bottom: 5px;
overflow-y: hidden;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
function appendText(text) {
$('#messages').append(text);
$('#messages').scrollTop($('#messages')[0].scrollHeight);
}
var ws = null;
var uriPieces = window.location.href.split('/');
var wsLocation = 'ws://' + uriPieces[2];
$('#uri:text').val(wsLocation);
$('#connect').click(function() {
ws = new WebSocket(uri.value);
ws.onopen = function(ev) {
appendText("Connected!\n");
};
ws.onclose = function(ev) {
appendText("Disconnected!\n");
};
ws.onmessage = function(ev) {
appendText(ev.data + "\n");
};
ws.onerror = function(ev) {
appendText("[error]\n");
console.log(ev);
};
});
$('#disconnect').click(function () {
ws.close();
});
$('#send').click(function () {
ws.send(sendMessage.value);
sendMessage.value = "";
});
$('#sendMessage').keyup(function(e) {
e.preventDefault();
if (e.keyCode === 13) {
$('#send').click();
}
});
});
</script>
</head>
<body>
<h1>Web Chat</h1>
Chat Server: <input id="uri" size="40">
<button id="connect">Connect</button>
<button id="disconnect">Disconnect</button><br />
<pre id="messages"></pre>
<div style="margin-bottom: 5px;">
Enter Message:<br />
<input id="sendMessage" size="80" value="" />
<button id="send">Send</button>
</div>
</body>
</html>