-
Notifications
You must be signed in to change notification settings - Fork 0
/
space-triangles.html
68 lines (53 loc) · 1.97 KB
/
space-triangles.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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Space Triangles</title>
<link rel="stylesheet" href="assets/css/style.css">
<!-- <link type="text/css" href="http://localhost:8000/assets/css/scrollbars.css" rel="stylesheet"/>
<script type="text/javascript" src="http://localhost:8000/assets/js/OverlayScrollbars.min.js"></script> -->
<script src="assets/js/main.js"></script>
</head>
<body>
<main id="elm-node"></main>
<script type="text/javascript">
// prevent backspace from redirecting browser
window.addEventListener('keydown',function(e){if(e.keyIdentifier=='U+0008'||e.keyIdentifier=='Backspace'||e.keyCode==8){if(e.target==document.body){e.preventDefault();return false;}}},true);
var socket;
var instanc;
var app = Elm.Main.init({node:document.getElementById('elm-node')});
function setupSocket() {
const urlParams = new URLSearchParams(window.location.search);
const room_name = urlParams.get('room');
socket = new WebSocket("ws://localhost:4000/ws/space-triangles/" + room_name)
socket.onclose = function() {
alert("Connection to the server has been lost.");
}
socket.onerror = function() {
alert("Server connection has experienced an unknown error.");
}
socket.onopen = function() {
app.ports.outputPort.subscribe(function (data) {
console.log("Message from client:")
console.log(data);
socket.send(data);
});
}
// reconnect on socket close?
socket.addEventListener("close", () => {
this.setupSocket()
})
// got message from server
socket.addEventListener("message", (event) => {
console.log("Message from server:" + event.data)
newJSON = JSON.parse(event.data)
console.log( newJSON );
app.ports.inputPort.send( newJSON );
})
}
window.onload = function() {
<!--setupSocket();-->
}
</script>
</body>
</html>