← index #10508Issue #8409
Off-topic · high · value 0.712
QUERY · ISSUE

Timeout support for websocket/webrepl

openby jimmoopened 2023-01-16updated 2024-09-12
enhancement

Reported here: https://github.com/orgs/micropython/discussions/10444

Webrepl constructs a websocket with BLOCKING_WRITE set. There's no timeout, so prints can hang.

CANDIDATE · ISSUE

WebREPL doesn't work with websocket in Typescript. 'OSError: Not a websocket request'

closedby bittervanopened 2022-03-13updated 2022-03-20

WebSocket on ESP32 Fails, 'Not a websocket request'


I've been working on build a visual studio code extension for a better developing experience on ESP32, MicroPython. But without any basic knowledge for front-end development, this process is quite tough for me.

To establish a wireless connection, the first thing that came into my mind is that I should check the official tools. So I checked the source code of project [webrepl](https://github.com/micropython/webrepl), a WebREPL terminal client, which also has a coarse front-end written in Javascript.

Problem

It seemed to be simple to connect to the webrepl server running on the board, as the code in [webrepl.html](https://github.com/micropython/webrepl/blob/master/webrepl.html) showed. But when I was using the npm package ws or websocket in Typescript to connect to it, the connection was simply cannot be established. The error in the serial connection illustrated that

Traceback (most recent call last):
  File "webrepl.py", line 43, in accept_conn
  File "websocket_helper.py", line 39, in server_handshake
OSError: Not a websocket request

WebREPL connection from: ('192.168.137.1', 2935)

At first I thought it was just like the old problems, parameters, policies, etc. So I applied WireShark to check the different between the packets sent by webrepl and the Typescript extension written by myself.

Using the ws package to connect:

3$9(N~% }8PF@I{)4S%RE8F

Using Javascript WebSocket to conncet:

FE_BGJ%T(MDXLD635QZTTB4

Having no idea which options to alter, I referred to the [MicroPython](https://github.com/micropython/micropython) project to find out. But the code shows that’s nothing to do with the options.

webkey = None

while 1:
    l = clr.readline()
    if not l:
        raise OSError("EOF in headers")
    if l == b"\r\n":
        break
    #    sys.stdout.write(l)
    h, v = [x.strip() for x in l.split(b":", 1)]
    if DEBUG:
        print((h, v))
    if h == b"Sec-WebSocket-Key":
        webkey = v

if not webkey:
     raise OSError("Not a websocket request")

But how can webkey == None happen? The two packets both have Sec-WebSocket-Key in their fields.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied