Skip to content

Instantly share code, notes, and snippets.

@kkestell
Created November 10, 2011 14:49
Show Gist options
  • Save kkestell/1355017 to your computer and use it in GitHub Desktop.
Save kkestell/1355017 to your computer and use it in GitHub Desktop.
A Simple Chat Room with Node.js
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="/client/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = new io.Socket(null, {rememberTransport: false, port: 8080});
socket.on('connect', function() {
socket.send('A client connected.');
});
socket.on('message', function(message) {
$('div#messages').html('<p>' + message + '</p>' + $('div#messages').html());
});
socket.on('disconnect', function() {
});
socket.connect();
$('input').keydown(function(event) {
if(event.keyCode === 13) {
socket.send($('input').val());
$('input').val('');
}
});
});
</script>
</head>
<body>
<input type="text" style="width: 300px;" />
<hr />
<div id="messages" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment