Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroWebSrv2 on W5500-EVB-Pico for WebSockets #92

Open
philippebourcier opened this issue Feb 11, 2023 · 4 comments
Open

MicroWebSrv2 on W5500-EVB-Pico for WebSockets #92

philippebourcier opened this issue Feb 11, 2023 · 4 comments

Comments

@philippebourcier
Copy link

philippebourcier commented Feb 11, 2023

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 (🤷‍♂️) :

  • socket.settimeout(0)
  • socket.setblocking(0)
  • srvSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    (I guess the W5500 handles this on its side anyway...)

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
Copy link
Author

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
Copy link
Owner

jczic commented Feb 13, 2023

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
Copy link
Author

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
Copy link
Owner

jczic commented Feb 13, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants