B4J Question Handling array of websockets

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I have the following code:

B4X:
Dim ws(19) As WebSocketClient
    For i = 0 To ws.Length -1
    ws(i).Initialize ("ws")
    If ws(i).Connected Then ws(i).close
        ws(i).Connect (url)
    Log (i)
    Next
    Wait For ws_Connected

What I cannot get my head around is how to capture each Ws_Connected event per websocket in the array, see the last line.
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
How about moving line 8 inside the loop. That way each web socket will connect individually.

The way you have it now, you will only get one WS_CONNECTED for one of the sockets, no guarentee which one.

Alternatively, you could move the WS_CONNECTED to its own sub.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thanks Andrew, you are completely right. That shows me when a websocket is connected - so far so good. But how do I know which one is connecting, and do I need to make a separate Sub to find out?

And also, how would I apply the below afterwards so it covers all the arrays? I mean, I get the content from all of them and luckily I don´t need to know for which one the message is received. But the Do While part cannot be used here, since I will have to address the specific websocket in the array, which makes no sense to do, since I want to know if any of the sockets gets disconnected but still keep the connection for the others.

B4X:
Do While ws.Connected

        Wait For ws_Textmessage (Message As String)
      
        Loop

I guess it´s a matter of having a Sub WS_Connected here, as you suggested, and then just make an endless loop (Do while 1 = 1 for instance) instead of the above, where the messages are received through the sockets. But how do I know in Sub Ws_Connected which websocket that gets disconnected?

Sorry, but I´m a noob when it comes to setting stuff like this up as an array.
 
Last edited:
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
The best way to handle such cases is with multiple instances of a class that handles a single connection.

Example: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73792/#content
Thanks Erel, that looks interesting and a bit complicated, but I will give it a try.

EDIT: I thought I would try to do this in a quicker way, having realized that all I really need to know is when a socket is being closed, so that I can reestablish the connection. It may not be superelegant, but also not cause any CPU-overload as I´m not expecting to see sockets being closed very often and I´ll only have 8 sockets open. I ended up with this, which I believe should be sufficient. Obviously it will point to some code which reestablishes the connection on this particular socket at a later point rather than just logging the closed socket:

B4X:
Sub ws_Closed (reason As String)
    
For i = 0 To ws.length - 1
    If ws(i).connected = False Then Log ("socket " & i & " was closed")
Next
End Sub
 
Last edited:
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
It good that you solved it however don't think that not using classes provides better performance.
Thanks Erel, I´m sure you´re right, so I will indeed look into this later. Is the websockethandler such a class or is the best way to follow the example you provided?
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
WebSocketHandler is a class that implements the protocol that B4J WebSocket servers expect. Worth going over its code.
Thanks, but it´s not necessarily making anything more efficient? I understand well how to communicate with websocket servers using the WebSocketClient library directly.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…