jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
654 stars 97 forks source link

MicroWebSrv2 on W5500-EVB-Pico for WebSockets #92

Open philippebourcier opened 1 year ago

philippebourcier commented 1 year ago

I am trying to get websockets to work on a W5500-EVB-Pico. I need Ethernet (vs WiFi), because it will be for a low-latency project where I'll need

In XAsyncSockets.py, I had to comment the following 3 lines (🤷‍♂️) :

At this point, HTTP works just fine...

Then, I wrote a simple test script for the ws client (tested succesfully on an ESP-32) :

from time import sleep
ws = create_connection("ws://192.168.98.120/")
while True:
    print("Sending 'Hello, World'...")
    ws.send("Hello, World")
    print("Received '%s'" % ws.recv())
    sleep(1)
ws.close()

However, on the console I see the accept, but it seems the code isn't able to detect it's a text or binary input (or something else is going wrong)... MWS2-DEBUG> From 192.168.98.11:80 GET / >> [101] Switching Protocols MWS2-INFO> WebSocket accepted from 192.168.98.11:80. WS ACCEPT

==> After receiving empty response I get a "Connection reset by peer" on the client side...

If I try again, same result : MWS2-DEBUG> From 192.168.98.11:80 GET / >> [101] Switching Protocols MWS2-INFO> WebSocket accepted from 192.168.98.11:80. WS ACCEPT

Anyone achieved to get WebSockets working on RP2040 ?

@jczic, I can lend you a W5500-EVB-Pico if you want to test it.

philippebourcier commented 1 year ago

I switched to a nightly build of micropython (official) and now I don't need to comment the 3 socket settings... However, I am still blocked at WS ACCEPT. 🤷‍♂️

jczic commented 1 year ago

Hi @philippebourcier and thank you for using this web server 👍🏻

Well, do you have trying the example with main.py file ? So, to correctly receive websocket message, you must to set callback functions in the OnWebSocketAccepted handler. Example:

def OnWebSocketAccepted(microWebSrv2, webSocket) :
    print('WebSocket accepted:')
    webSocket.OnTextMessage     = OnWebSocketTextMsg
    #webSocket.OnBinaryMessage = OnWebSocketBinaryMsg
    #webSocket.OnClosed               = OnWebSocketClosed

def OnWebSocketTextMsg(webSocket, msg) :
    print('WebSocket text message: %s' % msg)

wsMod.OnWebSocketAccepted = OnWebSocketAccepted

Also, do you have checked if you can receive a msg sent by the server to your javascript? Or do you think that the problem come from XAsyncSocket lib on your pico board?

philippebourcier commented 1 year ago

OK, looks like I had copied a bad example (SendText vs SendTextMessage, etc. 🤦‍♂️)... 🤷‍♂️ Now everything works fine. However, I am not really convinced in terms of stability and latency seems a bit high. 😬

def OnWebSocketAccepted(microWebSrv2, webSocket) :
    webSocket.OnTextMessage = OnWebSocketTextMsg

def OnWebSocketTextMsg(webSocket, msg) :
    webSocket.SendTextMessage(msg)

mws = MicroWebSrv2()
mws.BindAddress           = ('0.0.0.0',80)
mws.SetEmbeddedConfig()
wsMod = MicroWebSrv2.LoadModule('WebSockets')
wsMod.OnWebSocketAccepted = OnWebSocketAccepted
mws.StartManaged()

while True: sleep(1)

mws.Stop()

When the browser idles, the websocket server becomes unavailable (socket seems closed)... Is there something to run to clear idle/unused connections and/or relaunch the server automatically ?

jczic commented 1 year ago

You mean that when the browser stays connected with a javascript websocket that doesn't transmit, the server on the card doesn't accept other websockets? I'm not sure I understand in fact.

There are many parameters that can be configured indeed, I have to understand your problem because all the interactions with the sockets in the server are managed by my XAsyncSockets lib (with event management, buffers, concurrency and parralelism).