Skip to content

Instantly share code, notes, and snippets.

@abronte
Created July 15, 2011 22:28
Show Gist options
  • Select an option

  • Save abronte/1085702 to your computer and use it in GitHub Desktop.

Select an option

Save abronte/1085702 to your computer and use it in GitHub Desktop.
break it down
var nickname = "";
var socket;
//send the chat server your nickname and close the dialog box
function joinChat()
{
socket.emit('join', nickname);
$("#nick-modal").dialog('close')
}
//this is called when a message is sent to the "userlist" socket channel.
//it updates the #userlist div with the array of username's it received
function updateUsers(data)
{
html = "";
for(var i=0;i<data.length;i++) {
html += '<span id="user_'+data[i]+'">'+data[i]+'<br/></span>'
}
$("#userlist").html(html);
}
//
// $(function() {}) i
// this is a self executing function, it's equivalent of $(document).ready(function() {});
$(function() {
//socket IO library stuff
//wouldn't worry too much about this
socket = io.connect();
socket.on('chat', function (data) {
console.log(data.user+': '+data.msg);
});
socket.on('userlist', updateUsers);
//this is a dialog box from jquery ui
$("#nick-modal").dialog({
height: 120,
width: 225,
modal: true,
autoOpen: true,
draggable: false,
resizable: false,
closeOnEscape: false,
buttons: {
"Join": function() {
nickname = $("#nick-modal input").val();
joinChat();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment