docs: websocket
Documentation URL
https://docs.micropython.org/
Description
Where is the built in websocket module documentation?
Code of Conduct
Yes, I agree
aiohttp: fix header problem
Latest version of aiohttp:
When making connection to a Websocket, the header argument is ignored.
The consequence is that you cannot make a connection to most online MQTT broker's over websocket
because they need the header entry: "Sec-WebSocket-Protocol":"mqtt" in the handshake of
the upgrade protocol.
See this small example code:
It connects to a MQTT broker and then sends the CONNECT mqtt packet.
Then it should get a reply of opcode:2, data: b' \x02\x00\x00' where 'data' is a CONNACK mqtt package
Because of the missing header entry "Sec-WebSocket-Protocol":"mqtt" most brokers will refuse the connection or
refuse to accept MQTT packets.
import aiohttp
import asyncio
async def connect():
url = "ws://test.mosquitto.org:8080"
headers = {"Sec-WebSocket-Protocol":"mqtt"}
connect_pkt = bytearray(b'\x10\x10\x00\x04MQTT\x04\x02\x00x\x00\x04fz54')
async with aiohttp.ClientSession(headers=headers).ws_connect(url) as ws:
print("Connected")
await ws.send_bytes(connect_pkt)
opcode, data = await ws.ws.receive()
print(f"opcode:{opcode}, data{data}")
asyncio.run(connect())
I've just tested this and it works as expected, hopefully it is merged for the next release, thanks! 👍🏼
Thanks for updating. I tested the example code above and it works with this PR.