Android Question How to debug Websocket Client Library?

PirateWolf

New Member
Hello, B4A community. I'm trying to implement a mobile app client that uses Websocket protocol to communicate with a Python FastAPI server. I'm using Websocket Client Library for this. However, when I send a JSON text with base64 encoded images in its data, my app won't receive the message and instead a MessageToast will show saying "Websocket protocol violation". The message is very vague and I would like to know what exactly is causing this.

One thing to note is that this only happens when I attempt to receive base64 encoded images from the server. In every other case it works as expected.

WebsocketHandler implementation:
Sub Class_Globals
    Public ws As WebSocket
    Private CallBack As Object
    Private EventName As String
End Sub

Public Sub Initialize (vCallback As Object, vEventName As String)
    CallBack = vCallback
    EventName = vEventName
    ws.Initialize("ws")
End Sub

Public Sub Connect(Url As String)
    ws.Connect(Url)
End Sub

Public Sub Close
    If ws.Connected Then ws.Close
End Sub

'Raises an event on the server. The Event parameter must include an underscore
Public Sub SendEventToServer(Event As String, Data As Map)
    Dim m As Map
    m.Initialize
    m.Put("type", "event")
    m.Put("event", Event)
    m.Put("params", Data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    ws.SendText(jg.ToString)
End Sub

Private Sub ws_TextMessage(msg As String)
    Try
        CallSub2(CallBack, EventName & "_TextMessage", msg)
    Catch
        Log("TextMessage Error: " & LastException)
    End Try
End Sub

Private Sub ws_Connected
    CallSub(CallBack,  EventName & "_Connected")
End Sub

Private Sub ws_Closed (Reason As String)
    CallSub2(CallBack, EventName & "_Closed", Reason)
End Sub

Callbacks:
Sub wsh_Connected
    UpdateStatus
End Sub

Sub wsh_Closed (Reason As String)
    UpdateStatus
    ToastMessageShow(Reason, True)
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
don't use toast for debugging purposes. ever.

when the connection closes, there is nothing in the log?

large image files, made even larger by converting to base64, could cause the problem. see if increasing the frame size on your server helps. really large files might have to be chunked, but try with the frame size first. technically, there is no limit to the size, but the default maybe be too small for your purposes. you might want to implement some logging on the server to indicate the size of the base64 string it's about to send. this could give you an idea of where you need to draw the line as to the size of image files that can be stored for transmission later.

the websocket client library is a little vague as to what it reports (it can only do what autobahn exposes). you are not the first to complain about the exact meaning of "protocol violation". there could be a newer version of autobahn's client, the protocol referred to may not be the websocket protocol itself, but
rather a sub-protocol, of which there are several, etc. and the expression "protocol violation" does not appear anywhere in the published protocol (rfc5455, latest version 13, 2023-08-14).
 
Last edited:
Upvote 0
Top