B4J Question [Solved] B4JPackager11 / websocket project problem

bdunkleysmith

Active Member
Licensed User
Longtime User
As recommended by @Erel in this thread, I have commenced migrating the communications in my project from http to websocket, however it's not been as simple as I thought it would be.

In replacing the http based comms part of my project I hit some "roadblocks". So created a simple project to eliminate other potential sources of the problems in order to just test the server/client comms. I was able to establish the required two way comms between my simple B4J client and the B4J server when run within the IDE, however when I create an executable using B4JPackager11, the client immediately disconnects after attempting a websocket connection to the server.

I ported the simple B4J client to B4A. That performs as expected by connecting and consuming the data periodically sent from the server via the websocket connection. For clarity below is the class called by the websocket call to the server.

So this confirms the functional validity of my server, but with no errors logged when I try to make the connection after launching the client via run_debug.bat, I've not been able to determine the cause of the problem, importantly why it runs OK in the IDE, but not when packaged.

I've attached both the B4J and B4A client projects which should confirm they are pretty much identical, but I'd be interested to know if anyone has encountered any problems with websocket based projects packaged with B4JPackager11.

B4X:
'WebSocket class
Sub Class_Globals
    Private ws As WebSocket
    Private trg, Timer1 As Timer
    Private GameID As String
End Sub

Public Sub Initialize
   
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    Log("Connected")
    ws = WebSocket1
    Timer1.Initialize("timer1", 1000)
    Timer1.Enabled = True  
End Sub

Sub RequestForGameID_Data(Params As Map)
    GameID = Params.Get("gameid")
    Log("Request received for data from game with GameID: " & GameID)
    If Main.updateStatus.Get(GameID) = Null Then    'Game data has yet to commence being sent & so create map entry for game with status as "False"
        Main.updateStatus.Put(GameID, "False")
    End If
    trg.Initialize("trg", 50)
    trg.Enabled = True
    WriteOutput
End Sub

Sub WriteOutput
    Dim Counter As Int = 0
    Do While True
        If Main.updateStatus.Get(GameID) = "True" Then
            Dim jg As JSONGenerator
            jg.Initialize(DBUtils.GameIDValues(Main.SQL1, GameID))      
            Log(jg.ToString & Chr(10))      
            ws.RunFunction("Text", Array As Object(jg.ToString & Chr(10)))
            ws.Flush
            Main.updateStatus.Put(GameID, "False")
            Counter = -1
        End If  
        wait for trg_Tick
        If Counter = 100 Then
            ws.RunFunction("Text", Array As Object($"{"type":"connection","status":"CONNECTED"}"$ & Chr(10)))
            ws.Flush  
            Log($"{"type":"connection","status":"CONNECTED"}"$ & Chr(10)) 'Send heart beat at least every 5s
            Counter = -1
        End If
        Counter = Counter + 1
    Loop
End Sub


Sub WebSocket_Disconnected
    Log("Disconnected")
End Sub

Sub Device_Message(Params As Map)
    Log("Device message: " & Params.Get("message"))
End Sub

Sub Timer1_Tick
    Log(DateTime.Time(DateTime.Now))
    ws.RunFunction("ServerTime", Array As Object(DateTime.Time(DateTime.Now)))
    ws.Flush
End Sub
 

Attachments

  • WebSocketExample.zip
    8.9 KB · Views: 349
  • B4JWebsocketExample.zip
    3.8 KB · Views: 332

bdunkleysmith

Active Member
Licensed User
Longtime User
Many thanks @Erel

That's fixed that obstacle and so now I move onto the next. But with plenty of time available due to Lockdown 2.0 here in Melbourne, Australia, B4X is keeping me busy!
 
Upvote 0
Top