Implement controller and viewer communication using WebSockets

This commit is contained in:
Vojtěch Káně
2019-06-28 15:18:16 +02:00
parent ba54156da9
commit 25caaedcdd
3 changed files with 98 additions and 2 deletions
+20 -1
View File
@@ -10,10 +10,12 @@ document.onmouseup = function(e) {
if (e.button === 0) {
mouseDown = false;
document.getElementById("slingshot").classList.add("ungrabbed");
sendNudge(-Math.floor(e.pageX - window.innerWidth / 2), Math.floor(e.pageY - window.innerHeight / 2));
}
};
document.ontouchend = function(e) {
document.getElementById("slingshot").classList.add("ungrabbed");
sendNudge(-Math.floor(e.touches[0].clientX - window.innerWidth / 2), Math.floor(e.touches[0].clientY - (window.innerHeight / 2)));
};
document.ontouchstart = function(e) {
e.preventDefault();
@@ -47,4 +49,21 @@ document.addEventListener("touchmove", (e) => {
slingshot.style.width = distance({x: cursorX, y: cursorY}, {x: window.innerWidth/2, y: window.innerHeight/2}) + "px";
const angle = cursorX >= window.innerWidth/2 ? -Math.atan((window.innerHeight/2 - cursorY)/(window.innerWidth/2 - cursorX)) : (-Math.atan((window.innerHeight/2 - cursorY)/(window.innerWidth/2 - cursorX)) - Math.PI);
slingshot.style.transform = "rotate(" + angle + "rad)";
});
});
function sendNudge(x, y) {
fetch("http://localhost:8080", {
method: "POST",
headers: {"Content-Type": "application/json"},
referrer: "no-referrer",
body: JSON.stringify([x, y])
})
.then((response) => {
if (![200, 201, 204].includes(response.status)) {
throw new Error("Nudge request failed with http status: " + response.status)
}
})
.catch((e) => {
console.log("Failed to send nudge request " + e)
});
}