-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.js
91 lines (70 loc) · 2.35 KB
/
socket.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
const query = new URLSearchParams(document.location.search);
function include(file) {
let script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
function connect(events) {
let ip = query.get("ip") || "localhost";
let port = query.get("port") || 6557;
let socket = new WebSocket(`ws://${ip}:${port}/socket`);
socket.addEventListener("open", () => {
console.log("WebSocket opened");
socket.addEventListener("close", () => {
if (events["websocketClose"]) {
events["websocketClose"].call(events);
}
});
});
socket.addEventListener("message", (message) => {
let data = JSON.parse(message.data);
console.log ("-> " + data.event);
let event = events[data.event];
if (event) {
event.call(events, data);
}
});
socket.addEventListener("close", () => {
console.log("Failed to connect to server, retrying in 5 seconds");
setTimeout(() => { connect(events); }, 5000);
});
}
let widget = null, events = null;
document.addEventListener("DOMContentLoaded", () => {
let mode = query.get("prop");
if (mode == "saber") {
include("saber/saber.js");
include("saber/events.js");
setTimeout(function () {
widget = new Saber(query);
events = new Events(widget);
widget.setScaleStyle();
if (!query.get("test")) {
connect(events);
} else {
widget.showSaber();
}
}, 100);
} else {
include("notehistory/notehistory.js");
include("notehistory/events.js");
if (query.get("test")) include("notehistory/example.js");
setTimeout(function() {
widget = new NoteHistory(query);
events = new Events(widget);
widget.setScaleStyle();
if (!query.get("test")) {
connect(events);
} else {
for (let data of example) {
let event = events[data.event];
if (event) {
event.call(events, data);
}
}
}
}, 100);
}
});