MicroWebSrv2 WebSocket Client

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Peugot206
Posts: 31
Joined: Sat Apr 09, 2022 5:57 pm

MicroWebSrv2 WebSocket Client

Post by Peugot206 » Mon Apr 25, 2022 1:53 pm

Hi forum users,

I'm looking into the MicroWebSrv2 library (https://github.com/jczic/MicroWebSrv2) and I cannot figure out how to start a websocket client that connects to a localhost running webserver. All the examples seem to be based on being the server itself, rather than being a client. I found a user that seems to be using the library as a websocket client: https://github.com/jczic/MicroWebSrv2/issues/57, but he didn't post all his code. I currently have this code (copied relevant parts):

Code: Select all

from MicroWebSrv2 import *

def OnTextMessage(webSocket, msg):
    print(f'Received message: {msg} from socket {webSocket}, responding after 1s!')
    time.sleep(1)
    webSocket.SendTextMessage("Still working ESP32!")


def OnClosed(webSocket):
    print(f'Websocket {webSocket} closed..')


def OnWebSocketAccepted(microWebSrv2, webSocket):
    print('New WebSocket (myGreatChat proto) accepted from %s:%s.' % webSocket.Request.UserAddress)
    webSocket.OnTextMessage = OnTextMessage
    webSocket.OnClosed = OnClosed
    print(f'Started callbacks on websocket, sending message!')
    webSocket.SendTextMessage("Hi from ESP32!")


def start_ws():
    wsMod = MicroWebSrv2.LoadModule('WebSockets')
    wsMod.OnWebSocketAccepted = OnWebSocketAccepted
    # Instanciates the MicroWebSrv2 class,
    mws2 = MicroWebSrv2()
    mws2.BindAddress = (WS_IP, WS_PORT)
    # For embedded MicroPython, use a very light configuration,
    mws2.SetEmbeddedConfig()

    # All pages not found will be redirected to the home '/',
    mws2.NotFoundURL = '/'
    mws2.StartManaged(parllProcCount=1, procStackSize=10*1024)
    
start_ws()
This outputs:

Code: Select all

MWS2-INFO> Server listening on XXX.XXX.XXX.XX:8000.
MWS2-INFO> Starts the managed pool to wait for I/O events.
However, it never starts a connection to my websocket server, nor does it send any messages out. Could anyone maybe provide an example on how to do this? (if it helps, I'm using an ESP32).

Thanks for any help.

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: MicroWebSrv2 WebSocket Client

Post by karfas » Mon Apr 25, 2022 2:36 pm

Peugot206 wrote:
Mon Apr 25, 2022 1:53 pm
I'm looking into the MicroWebSrv2 library (https://github.com/jczic/MicroWebSrv2) and I cannot figure out how to start a websocket client that connects to a localhost running webserver. All the examples seem to be based on being the server itself
As far as I know, the websocket connections are always initiated by a client (see https://en.wikipedia.org/wiki/WebSocket).

The MicroWebSrv2 lib might have functions for dealing with the server part of ws:// URLs (I have no idea), but I'm sure it's not made to initiate ws:// connections.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Peugot206
Posts: 31
Joined: Sat Apr 09, 2022 5:57 pm

Re: MicroWebSrv2 WebSocket Client

Post by Peugot206 » Mon Apr 25, 2022 2:42 pm

karfas wrote:
Mon Apr 25, 2022 2:36 pm
Peugot206 wrote:
Mon Apr 25, 2022 1:53 pm
I'm looking into the MicroWebSrv2 library (https://github.com/jczic/MicroWebSrv2) and I cannot figure out how to start a websocket client that connects to a localhost running webserver. All the examples seem to be based on being the server itself
As far as I know, the websocket connections are always initiated by a client (see https://en.wikipedia.org/wiki/WebSocket).

The MicroWebSrv2 lib might have functions for dealing with the server part of ws:// URLs (I have no idea), but I'm sure it's not made to initiate ws:// connections.
Thanks for your answer. In this case my ESP32 is indeed the websocket client, so that should be the one to initiate the connection.

I think I have to search for another library then, do you know by any chance a good maintained library for MicroPython Websocket Clients?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: MicroWebSrv2 WebSocket Client

Post by karfas » Mon Apr 25, 2022 3:03 pm

No recommendation, but you could search google or this forum...
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply