Skip to content

Instantly share code, notes, and snippets.

@jafstar
Created November 12, 2011 08:37
Show Gist options
  • Save jafstar/1360254 to your computer and use it in GitHub Desktop.
Save jafstar/1360254 to your computer and use it in GitHub Desktop.
Simplest Chat Room - Client
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simplest Chat Room</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://theDomain.com');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
function sendMsg(){
var theMsg = $('#theMsg').val();
socket.emit('newMsg', { theMsg: theMsg });
$('#theMsg').attr('value','');
}
socket.on('writeMsg', function (data) {
console.log(data.theMsg);
$('#messages ul').append('<li>'+data.theMsg+'</li>');
});
</script>
</head>
<body>
<div id="messages">
<ul>
<li>Welcome to the chat room</li>
</ul>
</div>
<input type="text" id="theMsg">
<input type="button" value="Send" id="theBtn" onclick="sendMsg()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment