-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_1.html
More file actions
81 lines (68 loc) · 2.11 KB
/
example_1.html
File metadata and controls
81 lines (68 loc) · 2.11 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<canvas id="canvarino" width="800" height="300" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<h1>Status: <p style="display: inline" id="status_msg"></p></h1>
<script>
const c = document.getElementById("canvarino");
const ctx = c.getContext("2d");
const offset = 50
function drawCircle(ctx, x, y, color) {
ctx.beginPath();
ctx.arc(x, y, 20, 0, 2 * Math.PI);
ctx.fillStyle = color
ctx.fill();
}
function clearCanvas(ctx) {
ctx.clearRect(0, 0, 800, 300)
}
function drawCircles(arr) {
clearCanvas(ctx)
arr.forEach(circle => {
drawCircle(ctx, circle.pos_x, circle.pos_y, circle.color)
})
}
</script>
<script>
const status_msg = document.getElementById("status_msg");
const retry_cooldown = 5000
function connect() {
const socket = new WebSocket('ws://localhost:4000/socket/websocket/')
status_msg.innerText = "tentando conectar...";
socket.onopen = () => {
socket.send(
JSON.stringify({
"topic": "order_map_coords:lobby",
"event": "phx_join",
"payload": {},
"ref": "ref"
})
)
}
socket.onmessage = (event) => {
const msg = JSON.parse(event.data)
const payload = msg.payload.data
if (payload) {
status_msg.innerText = `recebido um pacote com ${payload.length} pedidos, atualizando o mapa.`
drawCircles(payload)
} else {
status_msg.innerText = "conectado com o socket, esperando updates";
}
}
socket.onclose = (ev) => {
status_msg.innerText = "conexão caiu, tentando se reconectar. Err code: " + ev.code;
setTimeout(function() {
connect();
}, retry_cooldown);
}
}
connect()
</script>
</body>
</html>