2013-10-16 20:41:47 +04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<title>Chat Example</title>
|
|
|
|
<script type="text/javascript">
|
2016-07-19 02:08:34 +03:00
|
|
|
window.onload = function () {
|
2013-10-16 20:41:47 +04:00
|
|
|
var conn;
|
2016-07-19 02:08:34 +03:00
|
|
|
var msg = document.getElementById("msg");
|
|
|
|
var log = document.getElementById("log");
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-07-19 02:08:34 +03:00
|
|
|
function appendLog(item) {
|
|
|
|
var doScroll = log.scrollTop === log.scrollHeight - log.clientHeight;
|
|
|
|
log.appendChild(item);
|
2013-10-16 20:41:47 +04:00
|
|
|
if (doScroll) {
|
2016-07-19 02:08:34 +03:00
|
|
|
log.scrollTop = log.scrollHeight - log.clientHeight;
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 02:08:34 +03:00
|
|
|
document.getElementById("form").onsubmit = function () {
|
2013-10-16 20:41:47 +04:00
|
|
|
if (!conn) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-19 02:08:34 +03:00
|
|
|
if (!msg.value) {
|
2013-10-16 20:41:47 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-19 02:08:34 +03:00
|
|
|
conn.send(msg.value);
|
|
|
|
msg.value = "";
|
2016-05-26 20:19:40 +03:00
|
|
|
return false;
|
2016-07-19 02:08:34 +03:00
|
|
|
};
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
if (window["WebSocket"]) {
|
|
|
|
conn = new WebSocket("ws://{{$}}/ws");
|
2016-05-26 20:19:40 +03:00
|
|
|
conn.onclose = function (evt) {
|
2016-07-19 02:08:34 +03:00
|
|
|
var item = document.createElement("div");
|
|
|
|
item.innerHTML = "<b>Connection closed.</b>";
|
|
|
|
appendLog(item);
|
2016-05-26 20:19:40 +03:00
|
|
|
};
|
|
|
|
conn.onmessage = function (evt) {
|
2016-07-19 02:08:34 +03:00
|
|
|
var messages = evt.data.split('\n');
|
2016-06-07 02:20:22 +03:00
|
|
|
for (var i = 0; i < messages.length; i++) {
|
2016-07-19 02:08:34 +03:00
|
|
|
var item = document.createElement("div");
|
|
|
|
item.innerText = messages[i];
|
|
|
|
appendLog(item);
|
2016-06-07 02:20:22 +03:00
|
|
|
}
|
2016-05-26 20:19:40 +03:00
|
|
|
};
|
2013-10-16 20:41:47 +04:00
|
|
|
} else {
|
2016-07-19 02:08:34 +03:00
|
|
|
var item = document.createElement("div");
|
|
|
|
item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
|
|
|
|
appendLog(item);
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-07-19 02:08:34 +03:00
|
|
|
};
|
2013-10-16 20:41:47 +04:00
|
|
|
</script>
|
|
|
|
<style type="text/css">
|
|
|
|
html {
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
body {
|
|
|
|
overflow: hidden;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
background: gray;
|
|
|
|
}
|
|
|
|
|
|
|
|
#log {
|
|
|
|
background: white;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0.5em 0.5em 0.5em 0.5em;
|
|
|
|
position: absolute;
|
|
|
|
top: 0.5em;
|
|
|
|
left: 0.5em;
|
|
|
|
right: 0.5em;
|
|
|
|
bottom: 3em;
|
|
|
|
overflow: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
#form {
|
|
|
|
padding: 0 0.5em 0 0.5em;
|
|
|
|
margin: 0;
|
|
|
|
position: absolute;
|
|
|
|
bottom: 1em;
|
|
|
|
left: 0px;
|
|
|
|
width: 100%;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="log"></div>
|
|
|
|
<form id="form">
|
|
|
|
<input type="submit" value="Send" />
|
|
|
|
<input type="text" id="msg" size="64"/>
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|