Created
March 9, 2016 09:41
-
-
Save kmansoft/41d6719cffdf781160af to your computer and use it in GitHub Desktop.
This file contains 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> | |
<meta charset="UTF-8"> | |
</head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js" type="text/javascript"></script> | |
<script src="./test-reconnecting-websocket.js"></script> | |
<script> | |
function log(msg) { | |
var node = document.createElement("li") | |
node.appendChild(document.createTextNode(msg)) | |
var parent = document.getElementById("log") | |
if (parent.firstChild) { | |
parent.insertBefore(node, parent.firstChild) | |
} else { | |
parent.appendChild(node) | |
} | |
} | |
var onOpen = function() { | |
console.log("Socket opened."); | |
log("Socket opened."); | |
}, | |
onClose = function() { | |
console.log("Socket closed."); | |
log("Socket closed."); | |
}, | |
onMessage = function(data) { | |
console.log("We get signal:", data); | |
var timeStamp = data.timeStamp | |
if (timeStamp > 1456696668595000) { | |
// https://bugzilla.mozilla.org/show_bug.cgi?id=960459 | |
timeStamp = timeStamp / 1000 | |
} | |
log(moment(timeStamp).format("LTS") + ":" + data.data) | |
}, | |
onError = function() { | |
console.log("We got an error."); | |
log("We got an error."); | |
} | |
var socket = new ReconnectingWebSocket("ws://127.0.0.1:8080/sub"); | |
socket.onopen = onOpen; | |
socket.onclose = onClose; | |
socket.onerror = onError; | |
socket.onmessage = onMessage; | |
</script> | |
<body> | |
<ul id="log" style="font: 14px Roboto Mono"></ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment