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