Skip to content

Commit 7d8c0b1

Browse files
committed
Made SSE and Websocket examples more visual
1 parent 4063b5a commit 7d8c0b1

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

docs/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ This might change in the future, but for now, it is recommended to use SSE only
283283

284284
.. literalinclude:: ../examples/httpserver_sse.py
285285
:caption: examples/httpserver_sse.py
286-
:emphasize-lines: 10,17,44-51,61
286+
:emphasize-lines: 10,17,46-53,63
287287
:linenos:
288288

289289
Websockets
@@ -302,7 +302,7 @@ This might change in the future, but for now, it is recommended to use Websocket
302302

303303
.. literalinclude:: ../examples/httpserver_websocket.py
304304
:caption: examples/httpserver_websocket.py
305-
:emphasize-lines: 9,16-17,60-67,76,81
305+
:emphasize-lines: 12,21,67-73,83,90
306306
:linenos:
307307

308308
Multiple servers

examples/httpserver_sse.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
<title>Server-Sent Events Client</title>
2424
</head>
2525
<body>
26+
<p>CPU temperature: <strong>-</strong>&deg;C</p>
2627
<script>
2728
const eventSource = new EventSource('/connect-client');
29+
const cpuTemp = document.querySelector('strong');
2830
29-
eventSource.onmessage = event => console.log('Event data:', event.data);
30-
eventSource.onerror = error => console.error('SSE error:', error);
31+
eventSource.onmessage = event => cpuTemp.textContent = event.data;
32+
eventSource.onerror = error => cpuTemp.textContent = error;
3133
</script>
3234
</body>
3335
</html>
@@ -58,5 +60,5 @@ def connect_client(request: Request):
5860
# Send an event every second
5961
if sse_response is not None and next_event_time < monotonic():
6062
cpu_temp = round(microcontroller.cpu.temperature, 2)
61-
sse_response.send_event(f"CPU: {cpu_temp}°C")
63+
sse_response.send_event(str(cpu_temp))
6264
next_event_time = monotonic() + 1

examples/httpserver_websocket.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# SPDX-License-Identifier: Unlicense
44

55
from time import monotonic
6+
import board
7+
import microcontroller
8+
import neopixel
69
import socketpool
710
import wifi
811

@@ -12,6 +15,8 @@
1215
pool = socketpool.SocketPool(wifi.radio)
1316
server = Server(pool, debug=True)
1417

18+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
19+
1520

1621
websocket: Websocket = None
1722
next_message_time = monotonic()
@@ -22,28 +27,30 @@
2227
<title>Websocket Client</title>
2328
</head>
2429
<body>
25-
<input id="message" type="text" placeholder="Message..."><br>
26-
<button id="send">Send</button>
27-
28-
30+
<p>CPU temperature: <strong>-</strong>&deg;C</p>
31+
<p>NeoPixel Color: <input type="color"></p>
2932
<script>
30-
const messageInput = document.querySelector('#message');
31-
const sendButton = document.querySelector('#send');
33+
const cpuTemp = document.querySelector('strong');
34+
const colorPicker = document.querySelector('input[type="color"]');
3235
3336
let ws = new WebSocket('ws://' + location.host + '/connect-websocket');
3437
3538
ws.onopen = () => console.log('WebSocket connection opened');
36-
ws.onerror = error => console.error('WebSocket error:', error);
37-
ws.onmessage = event => console.log('Received message from server: ', event.data);
38-
39-
let interval = setInterval(() => ws.send("Hello from client"), 1000);
40-
41-
ws.onclose = x => {
42-
console.log('WebSocket connection closed');
43-
clearInterval(interval);
44-
};
45-
46-
sendButton.onclick = () => ws.send(messageInput.value);
39+
ws.onclose = () => console.log('WebSocket connection closed');
40+
ws.onmessage = event => cpuTemp.textContent = event.data;
41+
ws.onerror = error => cpuTemp.textContent = error;
42+
43+
colorPicker.oninput = debounce(() => ws.send(colorPicker.value), 200);
44+
45+
function debounce(callback, delay = 1000) {
46+
let timeout
47+
return (...args) => {
48+
clearTimeout(timeout)
49+
timeout = setTimeout(() => {
50+
callback(...args)
51+
}, delay)
52+
}
53+
}
4754
</script>
4855
</body>
4956
</html>
@@ -73,10 +80,12 @@ def connect_client(request: Request):
7380

7481
# Check for incoming messages from client
7582
if websocket is not None:
76-
if (message := websocket.receive(True)) is not None:
77-
print("Received message from client:", message)
83+
if (data := websocket.receive(True)) is not None:
84+
r, g, b = int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16)
85+
pixel.fill((r, g, b))
7886

7987
# Send a message every second
8088
if websocket is not None and next_message_time < monotonic():
81-
websocket.send_message("Hello from server")
89+
cpu_temp = round(microcontroller.cpu.temperature, 2)
90+
websocket.send_message(str(cpu_temp))
8291
next_message_time = monotonic() + 1

0 commit comments

Comments
 (0)