B4J Question How To Tell Websocket Is Already Initialized

B4XDev

Active Member
Licensed User
I have a button a user can push to subscribe to additional channels (using websocket client). How do I tell after the first subscription is started that the websocket client is already up and running connected/initialized and doesn't need to be initialized again?

There is no isInitialized() method, so is it just a matter of using a Try...Catch?

I'm going to experiment in the meantime, but what's the preferred method?

EDIT: Clarified some terms
 
Last edited:
Solution
If you're using WebSocketClient there's a connected property:
B4X:
Private wsc As WebSocketClient

Public Sub isConnected As Boolean
    Return wsc.Connected
End Sub
Although I guess it might fail if the WebSocketClient hadn't been initialised?

teddybear

Well-Known Member
Licensed User
I can't get your question, what is that the websocket is already up and running? you mean using jserver or a b4j app?
 
Upvote 0

B4XDev

Active Member
Licensed User
I'm connecting to a remote server's websocket. It is not under my control.

I've modified my OP to more clearly state that I'm asking about the websocket client, not server.

I don't have to connect to the websocket until the user wants to open a channel. I want to see if the websocket has already been initialized.

It would also be helpful to know if the websocket was initialized prior to closing the app, as I attempt a websocket.close call.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I guess you do websocket at client. you have to do new websocket at first, but you may set a boolean as its initialized state after conneting success.
or you also check websocket.readyState to see if it has already been initialized.
 
Last edited:
Upvote 0

B4XDev

Active Member
Licensed User
...but you may set a boolean as its initialized state after conneting success.

Yes, a global var would do it, but that seems smelly.

or you also check websocket.readyState to see if it has already been initialized.

I can't find a readyState() on the websocket. I will do a search for it.

ADDED: There doesn't seem to be a readyState() for a websocket client. I did notice one available for websocket servers.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I can't find a readyState() on the websocket. I will do a search for it.

ADDED: There doesn't seem to be a readyState() for a websocket client. I did notice one available for websocket servers.
The readyState is a state of websocket, as I said, you have to new WebSocket at the first, you can do like this
B4X:
ws = new WebSocket("yoururl");
console.log(ws.readyState)
You can also test ws whether it is be defined or ws.readyState to see if it has already been initialized.

A way to do this is to initialize the websocket once after page onloaded, and then use it in send or onmessage event each time,you don't need to consider whether it has been initialized
 
Last edited:
Upvote 0

B4XDev

Active Member
Licensed User
The readyState is a state of websocket, as I said, you have to new WebSocket at the first, you can do like this
B4X:
ws = new WebSocket("yoururl");
console.log(ws.readyState)

I am looking for a B4X solution, not a Javascript solution.
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
If you're using WebSocketClient there's a connected property:
B4X:
Private wsc As WebSocketClient

Public Sub isConnected As Boolean
    Return wsc.Connected
End Sub
Although I guess it might fail if the WebSocketClient hadn't been initialised?
 
Upvote 0
Solution

B4XDev

Active Member
Licensed User
If you're using WebSocketClient there's a connected property:
B4X:
Private wsc As WebSocketClient

Public Sub isConnected As Boolean
    Return wsc.Connected
End Sub
Although I guess it might fail if the WebSocketClient hadn't been initialised?

Wait wait wait. How did I miss that?!!? Wow. I got tunnel vision on that one, for sure! Looking for "IsInitialized" and completely missed "Connected." Wow.

Thank you!

BTW: Using ws.Connected prior to ws.Initialize() seems to work just fine and provides the functionality I was needing.
 
Upvote 0
Top