uasyncio.websocket.server compatibility with websocket clients
server.py does not work with every clients. For instance CPython websockets client https://pypi.org/project/websockets/
I have made some experiments https://forum.micropython.org/viewtopic.php?f=15&t=6568&p=37374#p37374 and found that there is already some modifications to uncomment to enable compatibility with this client, plus pycopy port unable this modifications, with some explanation in the last PR. https://github.com/pfalcon/pycopy-lib/commit/3803ff6ad7156dee9575f1c921f4df3fa2bdc117
Is there a reason why it is not enabled ? does it broke compatibility with some other client ?
In this case could it be possible to selectively enable it depending the connected client?
Example for uasyncio.websocket.server fails
I need to run a websocket server on ESP32 and the official example raises the following exception when I connect from any client:
MPY: soft reboot
Network config: ('192.168.0.200', '255.255.255.0', '192.168.0.1', '8.8.8.8')
b'Sec-WebSocket-Version: 13\r\n'
b'Sec-WebSocket-Key: k5Lr79cZgBQg7irI247FMw==\r\n'
b'Connection: Upgrade\r\n'
b'Upgrade: websocket\r\n'
b'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n'
b'Host: 192.168.0.200\r\n'
b'\r\n'
Finished webrepl handshake
Task exception wasn't retrieved
future: <Task> coro= <generator object 'echo' at 3ffe79b0>
Traceback (most recent call last):
File "uasyncio/core.py", line 1, in run_until_complete
File "main.py", line 22, in echo
File "uasyncio/websocket/server.py", line 60, in WSReader
AttributeError: 'Stream' object has no attribute 'ios'
My micropython firmware and libraries:
- Micropython firmware: https://micropython.org/resources/firmware/esp32-idf3-20200902-v1.13.bin
- Pip libraries installed: micropython-ulogging, uasyncio.websocket.server
My main.py:
import network
import machine
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.ifconfig(('192.168.0.200', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
if not sta_if.isconnected():
print('connecting to network...')
sta_if.connect('my-ssid', 'my-password')
while not sta_if.isconnected():
machine.idle() # save power while waiting
print('Network config:', sta_if.ifconfig())
# from https://github.com/micropython/micropython-lib/blob/master/uasyncio.websocket.server/example_websock.py
import uasyncio
from uasyncio.websocket.server import WSReader, WSWriter
def echo(reader, writer):
# Consume GET line
yield from reader.readline()
reader = yield from WSReader(reader, writer)
writer = WSWriter(reader, writer)
while 1:
l = yield from reader.read(256)
print(l)
if l == b"\r":
await writer.awrite(b"\r\n")
else:
await writer.awrite(l)
import ulogging as logging
#logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
loop = uasyncio.get_event_loop()
loop.create_task(uasyncio.start_server(echo, "0.0.0.0", 80))
loop.run_forever()
loop.close()
Hello @damiencorpataux
Did you success run any example of uasyncio websocket server? I need to run a websocket server on ESP32 too, but I would like in secure mode, with SSL. But that official link in your example https://github.com/micropython/micropython-lib/blob/master/uasyncio.websocket.server/example_websock.py do not exists anymore. And, in new MicroPython structured I not found too :(
Thank you.
"Stream object has no attribute ios"
Not enough information to help sorry.
For async websocket server, see https://github.com/miguelgrinberg/microdot
@jimmo how to use upip to install microdot ?
@QGB upip has been essentially unsupported for a few years. I don't believe microdot has ever been installable by
upip.Coincidentally, I just published
upip's replacement,mip, a few days ago. Seehttps://github.com/miguelgrinberg/microdot/issues/67 for discussion about making it installable via
mip.In the meantime you can install manually by copying the required files (either just microdot.py, or both microdot.py and microdot_asyncio.py if you want the async version).
See
https://microdot.readthedocs.io/en/latest/intro.html#running-with-micropython
https://microdot.readthedocs.io/en/latest/extensions.html#asynchronous-support-with-asyncio