Moved chat example to chatserver.

This commit is contained in:
Nathan Osman
2015-07-09 11:40:15 -07:00
parent 26a44967e8
commit 6121a207ca
11 changed files with 23 additions and 28 deletions

View File

@@ -0,0 +1,46 @@
$(function() {
var index = -1,
$document = $(document),
$messages = $('.messages');
// Retrieve all messages after the specified index
function update() {
$.ajax({
type: 'POST',
url: '/api/getMessages',
data: JSON.stringify({index: index}),
contentType: 'application/json',
complete: function() {
window.setTimeout(update, 2000);
},
success: function(data) {
$.each(data.messages, function(i, e) {
$('<div>')
.addClass('message')
.text(e.message)
.appendTo($messages);
index = e.index;
});
$document.scrollTop(10000);
}
});
}
update();
// Find the message input box and set the appropriate handler for [enter]
var $input = $('#input').focus().keypress(function(e) {
if(e.which == 13) {
$.ajax({
type: 'POST',
url: '/api/postMessage',
data: JSON.stringify({message: $(this).val()}),
contentType: 'application/json'
});
// Clear the contents
$(this).val('');
return false;
}
});
});