Skip to content

Instantly share code, notes, and snippets.

@codemodify
Last active January 17, 2019 12:09
Show Gist options
  • Save codemodify/4f1d98ccb59a436a3e2815fd5b0a016c to your computer and use it in GitHub Desktop.
Save codemodify/4f1d98ccb59a436a3e2815fd5b0a016c to your computer and use it in GitHub Desktop.
qosmicparticles-io-samples.js
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
// XHR Example
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
var data = JSON.stringify({
"version": "2.0",
"key": "rcd1JN+CkZo2+KKR802bXTujubMbiZARQcyTR8Ku8haqdyaz8pA8Z1kbrWJO2J2CiwFdnr",
"gobSize": 10
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://qosmicparticles.io:4444/FetchGobs");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
// Streaming Example
// ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~
function setupWebSocket() {
var ws = new WebSocket("ws://qosmicparticles.io:4444/StreamGobs");
ws.onclose = function () {
var delay = 1000;
console.log("RETRY in " + delay + " ms")
setTimeout(setupWebSocket, delay);
};
ws.onopen = function (event) {
var apiKey = "rcd1JN+CkZo2+KKR802bXTujubMbiZARQcyTR8Ku8haqdyaz8pA8Z1kbrWJO2J2CiwFdnr"
var request = {
Version: "2.0",
Key: apiKey,
GobSize: 20
};
ws.send(JSON.stringify(request));
console.log("SEND: " + request);
};
ws.onmessage = function (event) {
console.log("RECV: " + event.data);
};
ws.onerror = function (event) {
console.log(event);
};
window.onbeforeunload = function (event) {
ws.close();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment