2018-06-14 14:40:35 -04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Meet the new anonkun.</title>
|
|
|
|
<script type="text/javascript" src="{{ url_for('static', filename='socket.io.slim.js') }}"></script>
|
|
|
|
<script>
|
|
|
|
var socket;
|
|
|
|
var tid = setInterval( function () {
|
|
|
|
if ( document.readyState !== 'complete' ) return;
|
|
|
|
clearInterval( tid );
|
|
|
|
|
|
|
|
socket = io.connect('https://' + document.domain + ':' + location.port + '/chat');
|
|
|
|
socket.on('connect', function() {
|
2018-06-14 18:49:01 -04:00
|
|
|
socket.emit('joined', {room: 'index'});
|
2018-06-14 14:40:35 -04:00
|
|
|
});
|
|
|
|
socket.on('message', function(data) {
|
2018-06-14 18:04:12 -04:00
|
|
|
date = new Date(data.date * 1000);
|
|
|
|
date_str = date.getFullYear() + '-' + padToTwo(date.getMonth()) + '-' + padToTwo(date.getDay()) + ' ';
|
|
|
|
date_str += padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds());
|
|
|
|
|
|
|
|
msg_str = '<div class="messageHeader">\n<span class="messageName">' + data.name + '</span> ';
|
|
|
|
msg_str += '<span class="messageDate">' + date_str + '</span>\n</div>\n';
|
|
|
|
msg_str += '<div class="message">' + data.message + '</div>\n';
|
|
|
|
|
|
|
|
mbox = document.getElementById('messageBox');
|
|
|
|
mbox.innerHTML = mbox.innerHTML + msg_str;
|
|
|
|
mbox.scrollTop = mbox.scrollHeight;
|
2018-06-14 14:40:35 -04:00
|
|
|
});
|
|
|
|
document.getElementById('text').addEventListener('keypress', function(event) {
|
|
|
|
if (event.key == 'Enter') {
|
|
|
|
text = document.getElementById('text').value;
|
|
|
|
document.getElementById('text').value = '';
|
2018-06-14 18:49:01 -04:00
|
|
|
socket.emit('message', {message: text, name: 'Anonymous', room: 'index'});
|
2018-06-14 14:40:35 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 100 );
|
2018-06-14 18:04:12 -04:00
|
|
|
|
|
|
|
function padToTwo(number) {
|
|
|
|
if (number<=99) { number = ("0"+number).slice(-2); }
|
|
|
|
return number;
|
|
|
|
}
|
2018-06-14 14:40:35 -04:00
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="document">
|
2018-06-14 18:04:12 -04:00
|
|
|
<h1>Chat</h1>
|
2018-06-14 14:40:35 -04:00
|
|
|
</div>
|
2018-06-14 18:04:12 -04:00
|
|
|
<div id="messageBox" style="height:120px; width:320px; border:1px solid #ccc; overflow:auto;">
|
2018-06-14 14:40:35 -04:00
|
|
|
{% for message in messages %}
|
|
|
|
<div class="message">
|
2018-06-14 18:04:12 -04:00
|
|
|
<div class="messageHeader">
|
|
|
|
<span class="messageName">{{ message["name"] }}</span> <span class="messageDate">{{ message["date"] | strftime }}</span>
|
|
|
|
</div>
|
2018-06-14 14:40:35 -04:00
|
|
|
<div class="messageMessage">{{ message["message"] }}</div>
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
<textarea id="text" rows="3" cols="43" style="resize:none"></textarea>
|
|
|
|
</body>
|
|
|
|
</html>
|