Skip to content

Instantly share code, notes, and snippets.

@maciejkorsan
Last active May 21, 2019 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maciejkorsan/5fcd22b4908e172205c224a8ee43c93b to your computer and use it in GitHub Desktop.
Save maciejkorsan/5fcd22b4908e172205c224a8ee43c93b to your computer and use it in GitHub Desktop.
import axios from "axios";
const hyperdrive = document.querySelector(".-hyperdrive");
const cockpit = document.querySelector(".-cockpit");
const headlights = document.querySelector(".-headlights");
let lights = {
cockpit: 0,
front: 0,
hyperdrive: 0
};
const syncanoInstance = process.env.SYNCANO_INSTANCE
const wsUrl = `wss://api.syncano.io/v2/instances/${syncanoInstance}/endpoints/sockets/chewbacca/global-messages/?transport=websocket`
const ws = new WebSocket(wsUrl);
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.payload.hasOwnProperty("cockpit")) {
lights = data.payload;
setLights(lights);
}
};
hyperdrive.addEventListener("click", e => {
axios.get(
`https://${syncanoInstance}.syncano.space/chewbacca/cockpit/?light=hyperdrive&status=1`
);
});
cockpit.addEventListener("click", e => {
axios.get(
`https://${syncanoInstance}.syncano.space/chewbacca/cockpit/?light=cockpit&status=${
lights.cockpit ? 0 : 1
}`
);
});
headlights.addEventListener("click", e => {
axios.get(
`https://${syncanoInstance}.syncano.space/chewbacca/cockpit/?light=front&status=${
lights.front ? 0 : 1
}`
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment