forked from TooTallNate/Java-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.html
More file actions
66 lines (59 loc) · 2.53 KB
/
chat.html
File metadata and controls
66 lines (59 loc) · 2.53 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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Chat Client</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
document.observe("dom:loaded", function() {
function log(text) {
$("log").innerHTML = (new Date).getTime() + ": " + (!Object.isUndefined(text) && text !== null ? text.escapeHTML() : "null") + $("log").innerHTML;
}
if (!window.WebSocket) {
alert("FATAL: WebSocket not natively supported. This demo will not work!");
}
var ws;
$("uriForm").observe("submit", function(e) {
e.stop();
ws = new WebSocket($F("uri"));
ws.onopen = function() {
log("[WebSocket#onopen]\n");
}
ws.onmessage = function(e) {
log("[WebSocket#onmessage] Message: '" + e.data + "'\n");
}
ws.onclose = function() {
log("[WebSocket#onclose]\n");
$("uri", "connect").invoke("enable");
$("disconnect").disable();
ws = null;
}
$("uri", "connect").invoke("disable");
$("disconnect").enable();
});
$("sendForm").observe("submit", function(e) {
e.stop();
if (ws) {
var textField = $("textField");
ws.send(textField.value);
log("[WebSocket#send] Send: '" + textField.value + "'\n");
textField.value = "";
textField.focus();
}
});
$("disconnect").observe("click", function(e) {
e.stop();
if (ws) {
ws.close();
ws = null;
}
});
});
</script>
</head>
<body>
<form id="uriForm"><input type="text" id="uri" value="ws://localhost:8887" style="width:200px;"> <input type="submit" id="connect" value="Connect"><input type="button" id="disconnect" value="Disconnect" disabled="disabled"></form><br>
<form id="sendForm"><input type="text" id="textField" value="" style="width:200px;"> <input type="submit" value="Send"></form><br>
<form><textarea id="log" rows="30" cols="100" style="font-family:monospace; color:red;"></textarea></form><br>
</body>
</html>