Skip to content

Instantly share code, notes, and snippets.

@hnq90
Last active August 29, 2015 14:06
Show Gist options
  • Save hnq90/d338dc2844319e4dc0f4 to your computer and use it in GitHub Desktop.
Save hnq90/d338dc2844319e4dc0f4 to your computer and use it in GitHub Desktop.
Demo Websocket with NodeJS & Socket.IO (Client-side)
<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