Last active
July 11, 2016 16:28
-
-
Save jazlalli/4604631 to your computer and use it in GitHub Desktop.
node.js chat 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
<script src="/jquery/1.4.4/jquery.min.js" ></script> | |
<script src="/socket.io/socket.io.js" ></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
$('#connect-button').click(function() { | |
$('#form-container').show(300); | |
var socket = io.connect('<hostname>', {port: 8888}); | |
socket.on('connect', function() { | |
socket.emit('set name', prompt('What is your name?')); | |
socket.on('ready', function() { | |
$('#connect-message').append($('<p>').text('Connected...')); | |
}); | |
}); | |
$('#simple-form').submit(function(e) { | |
e.preventDefault(); | |
var textObj = $('#data'); | |
var msg = textObj.val(); | |
textObj.val(''); | |
socket.emit('message', msg); | |
}); | |
socket.on('message', function(msg) { | |
$('#simple-form').append($('<p>').text(msg)); | |
}); | |
socket.on('disconnect', function(msg) { | |
$('#simple-form').append($('<p>').text(msg)); | |
}); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment