Last active
August 29, 2015 14:08
-
-
Save ernestlv/efc9ac7d2a0d4a463154 to your computer and use it in GitHub Desktop.
How to create a socket connection with Node.js - part two - the client side
This file contains hidden or 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> | |
<body> | |
<h1>Socket.io Test!!! :)</h1> | |
<form id="myForm"> | |
<input id="firstname" type="text"> | |
<input id="lastname" type="text"> | |
<input id="submit" type="submit"> | |
</form> | |
<script src="http://localhost:1337/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect("http://localhost:1337"), | |
fname = document.getElementById("firstname"), | |
lname = document.getElementById("lastname"), | |
form = document.getElementById("myForm"); | |
form.addEventListener("submit", function(e){ | |
e.preventDefault(); | |
socket.emit("myClientMessage", {firstname:fname.value, lastname:lname.value}); | |
}); | |
socket.on("myServerMessage", function(data){ //keep listening for server messages | |
if (data.username){ | |
alert("Username set: " + data.username); | |
}else{ | |
alert("Server Says: " + data); | |
} | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment