Created
February 12, 2017 19:01
-
-
Save JosephLivengood/41ba76348df3013b7870dc64861de744 to your computer and use it in GitHub Desktop.
FCC Advanced Node and Express Socket.IO Checkpoint 2 (Client)
This file contains 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
$( document ).ready(function() { | |
/*global io*/ | |
var socket = io(); | |
$('form').submit(function(){ | |
var messageToSend = $('#m').val(); | |
socket.emit('chat message', messageToSend); | |
$('#m').val(''); | |
return false; // prevent form submit from refreshing page | |
}); | |
socket.on('user', function(data){ | |
$('#num-users').text(data.currentUsers+' users online'); | |
var message = data.name; | |
if(data.connected) { | |
message += ' has joined the chat.'; | |
} else { | |
message += ' has left the chat.'; | |
} | |
$('#messages').append($('<li>').html('<b>'+ message +'</b>')); | |
}); | |
socket.on('chat message', function(data){ | |
$('#messages').append($('<li>').text(data.name+': '+data.message)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment