Created
November 25, 2012 05:45
-
-
Save jessecravens/4142531 to your computer and use it in GitHub Desktop.
O'Reilly HTML5 Hacks Chapter 9 - Connectivity - Hack #70
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 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