Skip to content

Instantly share code, notes, and snippets.

@jessecravens
Created November 25, 2012 05:45
Show Gist options
  • Save jessecravens/4142531 to your computer and use it in GitHub Desktop.
Save jessecravens/4142531 to your computer and use it in GitHub Desktop.
O'Reilly HTML5 Hacks Chapter 9 - Connectivity - Hack #70
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
WebSocketDemo = function(){
return {
ws: null,
init: function(url){
this.ws = new WebSocket(url);
this.onOpen();
this.onMessage();
this.onClose();
},
doSend: function(msg){
this.ws.send = function(evt) {
console.log(evt.timeStamp)
};
},
onOpen: function(){
this.ws.onopen = function(evt) {
console.log('CONNECTED: ' + evt.type);
WebSocketDemo.ws.send('html5 hacks');
};
},
onClose: function(){
this.ws.onclose = function(evt) {
console.log('CLOSED: ' + ': ' + evt.type);
};
},
onMessage: function(msg){
this.ws.onmessage = function(evt) {
console.log('RESPONSE: ' + ': ' + evt.data);
WebSocketDemo.ws.close();
};
}
}
}();
WebSocketDemo.init("ws://echo.websocket.org/");
</script>
</head>
<body>
<h2>WebSocket Test</h2>
<div id="output"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment