B4J Question Questions about jWebSocketClient WebSocketPush example

Chris2

Active Member
Licensed User
Regarding the example provided in the jWebSocketClient Library post - https://www.b4x.com/android/forum/threads/jwebsocketclient-library.40985/#content,
I have a couple of questions on the PushService module:
1. The WebSocketHandler appears to be declared twice, both in Process_Globals and the Connect sub:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private VERSION As Double = 1.0
    Private wsh As WebSocketHandler
    Private id As String
    Public kvs As KeyValueStore
    Private pingTimer As Timer
    Private lastServerPong As Long
    Type Message (id As String, Text As String)
    Private ReconnectTimer As Timer
End Sub

Private Sub Connect
    Dim wsh As WebSocketHandler
    wsh.Initialize(Me, "wsh")
    wsh.Connect(Main.serverLink)
End Sub
Is this correct, if so why?

2. In the wsh_Closed sub, CallSub2 is used rather than just referencing the Main module directly, is there a reason for this?
B4X:
Sub wsh_Closed (Reason As String)
    Log("WebSocket Closed: " & Reason)
    CallSub2(Main, "UpdateStatus", wsh.ws.Connected)    'why not Main.UpdateStatus(wsh.ws.Connected)
    pingTimer.Enabled = False
    ReconnectTimer.Enabled = True
End Sub

Many thanks.
 
Last edited:
Solution
1. When an object can be initialized multiple time, it is safer to first call Dim. Otherwise, the same object instance will be reinitialized and this can cause problems in some cases.

2. This is indeed not needed. It is an old cross platform code, based on B4A activities.

Chris2

Active Member
Licensed User
1. When an object can be initialized multiple time, it is safer to first call Dim. Otherwise, the same object instance will be reinitialized and this can cause problems in some cases.
So, would the following achieve the same thing?
B4X:
Private Sub Connect
    If Not(wsh.IsInitialized) Then wsh.Initialize(Me, "wsh")
    wsh.Connect(Main.serverLink)
End Sub
 
Upvote 0
Top