Open
Description
CircuitPython version and board name
Adafruit CircuitPython 9.2.4 on 2025-01-29; Wemos Lolin C3 Mini with ESP32-C3FH4
Code/REPL
import wifi
import socketpool
import asyncio
from adafruit_httpserver import Server, Request, Response, Websocket, GET
pool = socketpool.SocketPool(wifi.radio)
server = Server(
pool,
# root_path="/static",
https=True,
certfile="cert.pem",
keyfile="key.pem",
debug=True)
websocket: Websocket = None
@server.route("/ws", GET)
def connect_client(request: Request):
global websocket # pylint: disable=global-statement
if websocket is not None:
websocket.close() # Close any existing connection
websocket = Websocket(request)
return websocket
async def handle_http_requests():
while True:
server.poll()
await asyncio.sleep_ms(0)
async def handle_websocket_requests():
while True:
if websocket is not None:
if (data := websocket.receive(fail_silently=True)) is not None:
print(data)
await asyncio.sleep_ms(0)
async def send_websocket_messages():
while True:
if websocket is not None:
websocket.send_message("aaaa", fail_silently=True)
await asyncio.sleep_ms(1000)
server.start(str(wifi.radio.ipv4_address))
async def main():
await asyncio.gather(
asyncio.create_task(handle_websocket_requests()),
asyncio.create_task(send_websocket_messages()),
asyncio.create_task(handle_http_requests())
)
asyncio.run(main())
Behavior
When HTTPS is enabled (by providing certificate and key files), static content is served correctly; however, WebSocket connections fail. Testing with a C# Watson WebSocket client produces the error:
Traceback (most recent call last):
File "adafruit_httpserver/server.py", line 476, in poll
OSError: -30464
Traceback (most recent call last):
File "code.py", line 101, in <module>
File "asyncio/core.py", line 317, in run
File "asyncio/core.py", line 276, in run_until_complete
File "asyncio/core.py", line 261, in run_until_complete
File "code.py", line 98, in main
File "asyncio/funcs.py", line 162, in gather
File "asyncio/core.py", line 261, in run_until_complete
File "code.py", line 39, in handle_http_requests
File "adafruit_httpserver/server.py", line 528, in poll
File "adafruit_httpserver/server.py", line 476, in poll
OSError: -30464
This issue disappears when HTTPS is disabled (i.e., when the certificate-related parameters are commented out).
server = Server(
pool,
# https=True,
# certfile="cert.pem",
# keyfile="key.pem",
debug=True)
Expected Behavior:
WebSocket connections should be established successfully regardless of whether HTTPS is enabled, while still serving static content securely.
Actual Behavior:
With HTTPS enabled, WebSocket connections fail with the error OSError: -30464
during the polling process.
Additional Context:
- The issue appears to be isolated to WebSocket functionality when using HTTPS.
- The rest of the HTTP functionality (e.g., serving static content) is unaffected.
- This behavior was reproduced on a CircuitPython environment using the adafruit_httpserver library.