-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
128 lines (127 loc) · 4.42 KB
/
index.html
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
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tournament</title>
<!-- <link rel="shortcut icon" type="image/x-icon" href="/content/Werwolf_Bild.ico"> -->
<!-- <link rel="icon" type="image/svg+xml" href="/content/icon.svg" sizes="any"> -->
<script>
var mySockets = {};
function sendSocketCommand(wat) {
// console.log( "ssc: " + JSON.stringify(wat, null, 4));
if (wat.cmd == "connect")
{
// console.log("connecting!");
let socket;
try { socket = new WebSocket(wat.address); }
catch (e) {
console.error("open ws", e);
// app.ports.receiveSocketClose.send(
// { name : wat.name
// , code : 0
// , reason : "cannot create socket"
// }
// );
// return;
}
socket.onmessage = function (event) {
// console.log( "onmessage: " + JSON.stringify(event.data, null, 4));
socket.errorCounter = 0;
app.ports.receiveSocketMsg.send(
{ name : wat.name
, msg : "data"
, data : event.data
}
);
}
socket.onopen = function () {
if (socket.buffer) {
for (var i in socket.buffer)
socket.send(socket.buffer[i]);
delete socket.buffer;
}
}
socket.onclose = function (data) {
console.log(`ws closed: ${data.code}, ${data.reason}, counter=${socket.errorCounter||0}`);
console.log(data);
if (data.code == 1006 && data.reason == "" && socket.readyState != 3) {
if (!socket.errorCounter || socket.errorCounter < 3) {
socket.errorCounter = 1 + (socket.errorCounter || 0);
return;
}
}
app.ports.receiveSocketClose.send(
{ name : wat.name
, code : data.code
, reason : data.reason
}
);
delete mySockets[wat.name];
}
if (mySockets[wat.name]) {
mySockets[wat.name].close();
}
mySockets[wat.name] = socket;
}
else if (wat.cmd == "send")
{
// console.log("sending to socket: " + wat.name );
const socket = mySockets[wat.name];
if (socket.readyState == 1)
mySockets[wat.name].send(wat.content);
else if (socket.readyState == 0) {
socket.buffer = socket.buffer || [];
socket.buffer.push(wat.content);
}
}
else if (wat.cmd == "close")
{
// console.log("closing socket: " + wat.name);
if (mySockets[wat.name])
mySockets[wat.name].close();
delete mySockets[wat.name];
}
}
</script>
<script>
// (function () {
// const param = new URLSearchParams(window.location.search);
// if (!param.has("lang")) {
// var lang = navigator.language || navigator.userLanguage;
// lang = /^(\w+)[$\W]/.exec(lang)[1];
// param.set("lang", lang);
// window.location.search = param.toString();
// }
// })();
</script>
<script>
function copyToClipboard(text) {
const temp = document.createElement("input");
temp.setAttribute("value", text);
document.body.appendChild(temp);
temp.select();
document.execCommand("copy");
document.body.removeChild(temp);
}
</script>
</head>
<body>
<div id="elm">
</div>
<script src="/index.js"></script>
<script src="/js/local-storage.js"></script>
<script>
var app = Elm.Main.init( {
node: document.getElementById("elm")
});
if (document.getElementById("elm"))
{
document.getElementById("elm").innerText = 'This is a headless program, meaning there is nothing to show here.\\n\\nI started the program anyway though, and you can access it as `app` in the developer console.';
}
// Add this line!
app.ports.sendSocketCommand.subscribe(sendSocketCommand);
// app.ports.sendToClipboard.subscribe(copyToClipboard);
new ElmLocalStoragePorts().subscribe(app, "settingGetItem", "settingSetItem", "settingClear", "settingListKeys", "settingResponse");
</script>
</body>
</html>