iOS Question WebSocket Server Disconnect Issue

aaronk

Well-Known Member
Licensed User
Hi,

I am using the following code, and it's sending / receiving data fine etc. However if the server (B4J app) closes the WebSocket connection the B4i app seems to crash.

B4X:
'Class module
Sub Class_Globals
   
    Private mModule As Object 
    Private mEventName As String
   
    Dim ws As WebSocket
   
End Sub

Public Sub Initialize(pModule As Object, pEventName As String)
    mModule = pModule
    mEventName = pEventName
   
    ws.Initialize("ws")
   
End Sub

Private Sub ws_Connected
    Log("Connected")
End Sub

Private Sub ws_Closed (Reason As String)
    Log("Close - " & Reason)
End Sub

Public Sub SendMessageToCloud(Msg As String)
    SendEventToServer("Device_MessageFrom", CreateMap("text": Msg))
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

Sub SendToServer(msg As String)
   Dim data As Map
   data.Initialize
   data.Put("message", msg)
   SendEventToServer("cloud_message", data)
End Sub

Sub server_incoming(Params As List)
    Dim msg As String
        msg = Params.Get(0)
   
    Log(msg)
       
End Sub

Sub Disconnect
    ws.Close
End Sub

Sub Connect
    ' connect to WebSocket server
    ws.Connect("ws://127.0.0.1:8888/app")
End Sub

' Same as SendToServer
Sub SendData(msg As String)
    SendToServer(msg)
End Sub

Private Sub ws_TextMessage(msg As String)
    Try
        Dim jp As JSONParser
        jp.Initialize(msg)
        Dim m As Map = jp.NextObject
        Dim etype As String = m.get("etype")
        Dim params As List = m.get("value")
        Dim Event As String = m.get("prop")
        If etype = "runFunction" Then
            If Event = "server_incoming" Then
                server_incoming(params)
            End If
        End If
    Catch
        Log("TextMessage Error: " & LastException)
    End Try
End Sub

Error:
*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]
Stack Trace: (
CoreFoundation <redacted> + 154
libobjc.A.dylib objc_exception_throw + 38
CoreFoundation <redacted> + 298
CoreFoundation <redacted> + 44
Cloud Test -[B4IWebSocketDelegate webSocket:didCloseWithCode:reason:wasClean:] + 80
Cloud Test __27-[SRWebSocket _pumpWriting]_block_invoke + 154
libdispatch.dylib <redacted> + 10
libdispatch.dylib <redacted> + 22
libdispatch.dylib _dispatch_main_queue_callback_4CF + 890
CoreFoundation <redacted> + 8
CoreFoundation <redacted> + 1422
CoreFoundation CFRunLoopRunSpecific + 486
CoreFoundation CFRunLoopRunInMode + 104
GraphicsServices GSEventRunModal + 156
UIKit <redacted> + 574
UIKit UIApplicationMain + 150
Cloud Test main + 106
libdyld.dylib <redacted> + 2
)

The Sub ws_Closed doesn't seem to get triggered.

Any ideas on what I am doing wrong?

Using B4i Version 3.01 on Windows 10 (using Parallels desktop)
Local B4i Build Server Version 3.01
XCode Version 8.2.1 (8C1002)

When using the same code in B4J it seems to work fine.
 

aaronk

Well-Known Member
Licensed User
It should fix this issue.
It seems to fix it, but..

When the WebSocket is closed it now triggers the following code:
B4X:
Private Sub ws_Closed (Reason As String)
    Log("Close - " & Reason)
End Sub

However, it logs: Close - <null>

Should the reason in that sub return something other than <null> or is it not used ?
 
Upvote 0

Similar Threads

Top