-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket-test.html
More file actions
69 lines (58 loc) · 2.16 KB
/
Copy pathwebsocket-test.html
File metadata and controls
69 lines (58 loc) · 2.16 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
<style>
button {
font-size: 16px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background: #ddd;
padding: 1em 2em;
border: 1px solid #ccc;
border-radius: 5px;
margin: .5em;
}
.connectionstatus {
display: block;
width: 20px;
height: 20px;
border-radius: 50%;
background: red;
}
.connectionstatus.connected {
background: green;
}
</style>
<span class="connectionstatus"></span>
<button data-send-color data-color="0,0,0">Blackout!</button><br />
<br />
<button data-send-color data-color="255,0,0">Send red!</button><br />
<button data-send-color data-color="0,255,0">Send blue!</button><br />
<button data-send-color data-color="0,0,255">Send green!</button><br />
<br /><br />
<button data-send-color data-color="255,0,0" data-fade>Fade red!</button><br />
<button data-send-color data-color="0,255,0" data-fade>Fade blue!</button><br />
<button data-send-color data-color="0,0,255" data-fade>Fade green!</button><br />
<script src="https://cdn.jsdelivr.net/npm/socket.io-client@2/dist/socket.io.js"></script>
<script>
const socket = io('http://pixelpi.local:3030');
const statusEl = document.querySelector('.connectionstatus');
socket.on('connect', function () {
console.log('connected!');
statusEl.classList.add('connected');
});
socket.on('disconnect', function () {
console.log('disconnected!')
statusEl.classList.remove('connected');
});
socket.on('error', function (e) {
console.log('error!', e)
statusEl.classList.remove('connected');
});
[...document.querySelectorAll('[data-send-color]')].forEach(button => {
button.addEventListener('click', () => {
sendColor(button.dataset.fade, button.dataset.color.split(','));
});
});
function sendColor(fade, [r, g, b]) {
console.log('send color', fade, r,g,b);
const arr = new Array(300).fill([r, g, b]);
socket.emit(typeof fade !== undefined ? 'data-fade' : 'data', arr);
}
</script>