actionhero
Version:
The reusable, scalable, and quick node.js API server for stateless and stateful applications
81 lines (75 loc) • 2.78 kB
HTML
<html>
<head>
<script src="/public/javascript/ActionheroWebsocketClient.js"></script>
<script type="text/javascript">
//////////
// MAIN //
//////////
function main() {
// start with no sesionId
deleteAllCookies();
// make any api call to get a new sessionId
actionWeb({ action: "status" }, function (error, data) {
var cookieSessionId = document.cookie.split(";")[0].split("=")[1];
console.log("cookie sessionId: " + cookieSessionId);
console.log("Web sessionId: " + data.requesterInformation.id);
console.log(
"Web fingerprint: " + data.requesterInformation.fingerprint
);
// start the websocket client
client = new ActionheroWebsocketClient();
client.connect(function (error, details) {
console.log("WebSocket cleintId: " + client.id);
console.log("WebSocket fingerprint: " + client.fingerprint);
// There should be a 3-way match
console.log(
data.requesterInformation.fingerprint === cookieSessionId &&
client.fingerprint === cookieSessionId
);
});
});
}
/////////////
// HELPERS //
/////////////
var url = window.location.origin + "/api";
var deleteAllCookies = function () {
// http://stackoverflow.com/questions/179355/clearing-all-cookies-with-javascript
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
};
var actionWeb = function (params, callback) {
// dupliace of ActionheroWebsocketClient.actionWeb, but listed here for simplicity
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var response = JSON.parse(xmlhttp.responseText);
callback(null, response);
} else {
callback(xmlhttp.statusText, xmlhttp.responseText);
}
}
};
var qs = "?";
for (var i in params) {
qs += i + "=" + params[i] + "&";
}
var method = "GET";
if (params.httpMethod != null) {
method = params.httpMethod;
}
xmlhttp.open(method, url + qs, true);
xmlhttp.send();
};
</script>
</head>
<body onload="main()">
<p>Videw the console for details</p>
</body>
</html>