-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
151 lines (123 loc) · 3.81 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import 'https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js';
import QrScanner from "./libs/qr-scanner.min.js"; // if using plain es6 import
import './libs/hammer.min.js'
QrScanner.WORKER_PATH = "./libs/qr-scanner-worker.min.js";
const qrVideo = document.querySelector("#qrVideo");
const loginCode = document.querySelector("#loginCode");
const controllerPage = document.querySelector("#controllerPage");
const loginPage = document.querySelector("#loginPage");
const animatedScanner = document.querySelector(".animated-scanner-container");
const container = document.querySelector(".container");
const keyBtns = document.querySelectorAll(".key");
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js").then(function() {
console.log("Service Worker Registered");
});
}
if (workbox) {
console.log(`Yay! Workbox is loaded 🎉`);
} else {
console.log(`Boo! Workbox didn't load 😬`);
}
window.oncontextmenu = function(event) {
event.preventDefault();
event.stopPropagation();
return false;
};
let ws;
let wasSocketConnected = false;
let loginInfo;
const connectToServer = () => {
console.log(loginInfo);
ws = new WebSocket(`wss://keymote.creativeshi.com/ws/${loginInfo.code}`);
ws.onopen = e => {
wasSocketConnected = true;
controllerPage.style.display = "block";
loginPage.style.display = "none";
};
ws.onclose = e => {
console.log(
"Socket is closed. Reconnect will be attempted in 1 second.",
e.reason
);
if (wasSocketConnected) {
setTimeout(() => {
connectToServer();
}, 1000);
}
};
ws.onerror = err => {
console.error("Socket encountered error: ", err.message, "Closing socket");
ws.close();
};
};
const qrScanner = new QrScanner(qrVideo, result => {
console.log("decoded qr code:", result);
const info = JSON.parse(result);
loginInfo = info;
connectToServer();
qrScanner.stop();
});
QrScanner.hasCamera().then(() => {
qrScanner.start();
animatedScanner.style.display = "block";
//also show the animated scanner
});
loginCode.addEventListener("click", () => {
//stop the scan
qrScanner.stop();
animatedScanner.style.display = "none";
});
loginCode.addEventListener("keypress", e => {
//stop the scan
//if user presses enter then login
if (e.key === "Enter") {
loginInfo = { code: loginCode.value };
connectToServer();
}
});
// Create a manager to manager the element
const hammertime = new Hammer.Manager(container);
const Swipe = new Hammer.Swipe();
hammertime.add(Swipe);
//add swiping gestures to click on button
hammertime.on("swipeleft", e => {
keyBtns[1].click();
});
hammertime.on("swiperight", e => {
keyBtns[3].click();
});
hammertime.on("swipeup", e => {
keyBtns[0].click();
});
hammertime.on("swipedown", e => {
keyBtns[4].click();
});
const onlongtouch = msg => {
console.log("long touch");
const sendMsgRepeatedly = () => {
ws.send(JSON.stringify(msg));
msgTimer = setTimeout(sendMsgRepeatedly, 100);
};
sendMsgRepeatedly();
};
let timer;
let msgTimer;
const touchduration = 500; //length of time we want the user to touch before we do something
const touchstart = msg => {
timer = setTimeout(() => onlongtouch(msg), touchduration);
};
const touchend = () => {
//stops short touches from firing the event
clearTimeout(msgTimer);
if (timer) clearTimeout(timer); // clearTimeout, not cleartimeout..
};
keyBtns.forEach(el => {
el.addEventListener("click", () => {
//send the id of element up,down,left right
let keyInfo = { key: el.id, event: "down" };
ws.send(JSON.stringify(keyInfo));
keyInfo = { key: el.id, event: "up" };
ws.send(JSON.stringify(keyInfo));
});
});