Created
February 5, 2011 10:11
-
-
Save heroic/812348 to your computer and use it in GitHub Desktop.
node-mysql chat test file
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
<html> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> | |
<title>Page Title</title> | |
<script src="http://cdn.socket.io/stable/socket.io.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> | |
<script type="text/javascript" charset="utf-8"> | |
socket = new io.Socket('localhost',{ | |
port: 8080 | |
}); | |
socket.connect(); | |
socket.on('connect', function(){ | |
socket.send({ | |
user_id: 1 | |
}); | |
}); | |
$(function(){ | |
var textBox = $('.chat'); | |
textBox.parent().submit(function(){ | |
if(textBox.val() != "") { | |
//send message to chat server | |
socket.send({ | |
text: textBox.val() | |
}); | |
addToWindow(textBox.val()); | |
textBox.val(''); | |
return false; | |
} | |
}); | |
socket.on('message', function(msg){ | |
if(msg.text != null) { | |
addToWindow(msg.text); | |
} | |
}); | |
}); | |
function addToWindow(text) { | |
$('#text').append('<p>' + text + '</p>'); | |
} | |
</script> | |
</head> | |
<body> | |
<div id="text"></div> | |
<form method="post" action=""> | |
<input type="text" name="" class="chat" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment