Skip to content

Instantly share code, notes, and snippets.

@jazlalli
Last active July 11, 2016 16:28
Show Gist options
  • Save jazlalli/4604631 to your computer and use it in GitHub Desktop.
Save jazlalli/4604631 to your computer and use it in GitHub Desktop.
node.js chat client
<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