Last active
August 29, 2015 14:06
-
-
Save hnq90/d338dc2844319e4dc0f4 to your computer and use it in GitHub Desktop.
Demo Websocket with NodeJS & Socket.IO (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> | |
<head> | |
<title>WebSocket Client</title> | |
<script src="http://localhost:6666/socket.io/socket.io.js"></script> | |
<script> | |
window.onload = function(){ | |
var input = document.getElementById('input'); | |
var output = document.getElementById('output'); | |
var socket = io.connect('localhost',{port: 5555}); | |
socket.on("connect",function(){ | |
console.log("Connected to the server"); | |
}); | |
socket.on('message',function(data) { | |
output.innerHTML = '=' + data; | |
}); | |
socket.on('error', function (data) { | |
console.log("error:",data); | |
}); | |
window.sendMessage = function(){ | |
socket.send(input.value); | |
}; | |
} | |
</script> | |
</head> | |
<body> | |
<input type='text' id='input' /> | |
<span id='output'></span> | |
<br/> | |
<input type='button' id='send' value='calc' onclick='sendMessage();' /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment