Created
June 3, 2013 21:55
-
-
Save robertz/5701798 to your computer and use it in GitHub Desktop.
Javascript object for the chat demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var engine = { | |
delay: 5000 | |
,currentID: 0 | |
,init: function(){ | |
window.chat = {}; | |
window.chat.data = {}; | |
$('#send').on('click', engine.sendMessage); | |
$('#setName').on('click', engine.setUser); | |
$('#message').on('keypress', function(e){ | |
if(e.which == 13 && $('#message').val().length){ | |
engine.sendMessage(); | |
} | |
}); | |
if(!userName.length){ | |
$('#nameContainer').show() | |
} | |
else{ | |
$('#displayName').append(userName); | |
$('#messageContainer').show(); | |
engine.start(); | |
} | |
return; | |
} | |
,start: function(){ | |
engine.messageSubscriber(); | |
window.setInterval(engine.messageSubscriber, engine.delay); | |
return; | |
} | |
,setUser: function(){ | |
if($('#userName').val().length){ | |
$.ajax({ | |
url: '/api/chat/setuser', | |
data: { | |
userid: $('#userName').val() | |
}, | |
dataType: 'json', | |
success: function(data){ | |
if(data.svrStatus == '0'){ | |
userName = $('#userName').val(); | |
$('#displayName').append(userName); | |
$('#nameContainer').hide(); | |
$('#messageContainer').show(); | |
engine.start(); | |
} | |
else{ | |
alert(data.svrMessage); | |
} | |
} | |
}); | |
} | |
else{ | |
alert('For real?!? Nah, you\'re kidding. Enter a name.'); | |
} | |
return; | |
} | |
,messageSubscriber: function(){ | |
$.ajax({ | |
url: '/api/chat/list' | |
,method: 'POST' | |
,data: { | |
startID: engine.currentID | |
} | |
,dataType: 'json' | |
,cache: false | |
,success: function(data){ | |
window.chat.data = data; | |
engine.currentID = data.lastid; | |
engine.processPayload(data); | |
} | |
,error: function(){ | |
$('<div/>', { | |
text: 'Error interacting with /api/chat/list endpoint!' | |
}) | |
.addClass('message-error') | |
.appendTo('#chatDiv'); | |
} | |
}); | |
return; | |
} | |
,processPayload: function(payload){ | |
var messages = payload.messages | |
,clients = payload.clients; | |
// message handler | |
for(var i = 0; i < messages.length; i++){ | |
var message = messages[i]; | |
if(!$('#message-' + message.msgid).length){ //prevent occasional hiccups | |
switch(message.type){ | |
case 'message': | |
var out = '<span id="message-' + message.msgid + '" class="message user-message"><span class="timestamp">'; | |
out += message.timestamp.split(' ')[1] + ' ' + message.timestamp.split(' ')[2] + '</span>'; | |
out += message.userid + ': ' + message.message + '</span>'; | |
$('#chatDiv').append(out); | |
break; | |
case 'notice': | |
var out = '<span id="message-' + message.msgid + '" class="message notice-message"><span class="timestamp">'; | |
out += message.timestamp.split(' ')[1] + ' ' + message.timestamp.split(' ')[2] + '</span>'; | |
out += 'NOTICE:' + message.message + '</span>'; | |
$('#chatDiv').append(out); | |
break; | |
defaut: break; | |
} | |
} | |
// Set chatDiv scroll to max extent | |
$('#chatDiv').prop('scrollTop', $('#chatDiv').prop('scrollHeight')); | |
} | |
// client handler | |
$('#clientDiv').html(''); | |
for(i=0; i < clients.length; i++){ | |
$('#clientDiv').append('<div class="client-container"><i class="icon-user"></i> ' + clients[i].userid + '</div>'); | |
} | |
return; | |
} | |
,sendMessage: function(){ | |
$.ajax({ | |
url: '/api/chat/put' | |
,method: 'POST' | |
,data: { | |
userid: userName | |
,message : $('#message').val() | |
} | |
,dataType: 'json' | |
,cache: false | |
,success: function(){ | |
engine.messageSubscriber(); | |
} | |
,error: function(){ | |
$('<div/>', { | |
text: 'Error interacting with /api/chat/put endpoint!' | |
}) | |
.addClass('message-error') | |
.appendTo('#chatDiv'); | |
} | |
}); | |
$('#message').val(''); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment