lost with MicroWebSrv2 and web sockets

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
Posts: 8
Joined: Sat Oct 31, 2020 9:45 am

lost with MicroWebSrv2 and web sockets

Post by peter247 » Thu Nov 19, 2020 8:09 pm

Using the example how to use web sockets and understand it , BUT how do I get send a message out of the OnWebSocket_xx's
How do I get access to the WebSockets instance to send a message ? Because I will get a WebSocket is not defined .

Code: Select all

# web server stuff    
def OnWebSocketAccepted(microWebSrv2, webSocket) :
    print('Example WebSocket accepted:')
    print('   - User   : %s:%s' % webSocket.Request.UserAddress)
#   print('   - Path   : %s'    % webSocket.Request.Path)
    print('   - Origin : %s'    % webSocket.Request.Origin)
    webSocket.OnTextMessage   = OnWebSocketTextMsg
    webSocket.OnBinaryMessage = OnWebSocketBinaryMsg
    webSocket.OnClosed        = OnWebSocketClosed
# ============================================================================
def OnWebSocketTextMsg(webSocket, msg) :
    print('WebSocket text message: %s' % msg)
    webSocket.SendTextMessage('Received "%s"' % msg)
    if msg == "Up and ready :)":
        print("now we are talking")
        webSocket.SendTextMessage('Ready to send you my updates')
# ------------------------------------------------------------------------
def OnWebSocketBinaryMsg(webSocket, msg) :
    print('WebSocket binary message: %s' % msg)
# ------------------------------------------------------------------------
def OnWebSocketClosed(webSocket) :
    print('WebSocket %s:%s closed' % webSocket.Request.UserAddress)
# ============================================================================

# Loads the WebSockets module globally and configure it,
wsMod = MicroWebSrv2.LoadModule('WebSockets')
wsMod.OnWebSocketAccepted = OnWebSocketAccepted

mws2 = MicroWebSrv2()
*** I have worked out my way in ***

So everything as to be within the OnWebSocketAccepted function ? , So I have set up a timer which I can use as a poll or read and post my sensor data .
It would have been better if that was an example

Post Reply