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()
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.
Thanks for any help.