Skip to content

Instantly share code, notes, and snippets.

@sadah
Created January 20, 2013 22:09
Show Gist options
  • Select an option

  • Save sadah/4582144 to your computer and use it in GitHub Desktop.

Select an option

Save sadah/4582144 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(function(){
var host = "ws://localhost:8888/";
var socket = new WebSocket(host);
console.log(socket.readyState);
socket.onopen = function(){
console.log(socket.readyState);
}
socket.onmessage = function(message){
console.log(socket.readyState + " " + message.data);
}
socket.onclose = function(){
console.log(socket.readyState);
}
$(window).unload(function() {
socket.onclose();
console.log(socket.readyState);
})
$("#sendBtn").on("click",function(){
message = $("#message").val()
socket.send(message);
});
})
</script>
</head>
<body>
<h1>Hello World!</h1>
<input type="text" id="message" /><button id="sendBtn">send</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment