B4J Question [SOLVED] WebServer: error only on client first connection

LucaMs

Expert
Licensed User
Longtime User
The webserver reports this error:

java.lang.NullPointerException
at anywheresoftware.b4j.object.WebSocketModule$Adapter.onWebSocketText(WebSocketModule.java:121)



when my b4a client app starts the first time.
I close the app and restart it and i do not get that error message, all works well.

Note that the connection is successful in both cases, but on app first start the subsequent requests to the server do not work and do not return errors on the server.

(my bad english :mad:)
 

Straker

Active Member
Licensed User
Longtime User
Can you add the code where you initialize the connection?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Can you add the code where you initialize the connection?

I did a test with no frills.

After the first client compile and run (and therefore its connection), there will be that error message on the server. Running again the client app, the message will not be there and both sides will receive a text message.



[Molto interessante la tua PrivacyCard. Ho letto che vorresti che il tutto rimanesse in Italia, ma ugualmente ti consiglierei di mettere anche la versione inglese dei testi del sito.

Very interesting your PrivacyCard. I have read that you would like that the whole thing remained in Italy, but I recommend you to also put the English version of the site]
 

Attachments

  • Client.zip
    14.3 KB · Views: 303
  • Server.zip
    1.6 KB · Views: 263
Last edited:
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Mmmmm... I checked your code.
When you start the service SrvCommun you chect the wsh websocket. If it is not initialized od not connected you call a Sub Connect.

B4X:
Private Sub Connect
modUtils.LogSub("srvCommun", "Connect")
    wsh.Initialize(Me, "wsh")
    wsh.ws.Connect(Main.ServerLink)
End Sub

This sub performs wsh.ws.initialize (and set the callback) and then open the connection. But I think you shoud re-check the wsh.isInitialized status before the connect...
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Are you sending any message from the client immediately after the connection? If yes then try to use CallSubDelayed in the client code to send the message only after the connection event.

B4X:
Public Sub wsh_Connected
modUtils.LogSub("srvComm", "wsh_Connected")
    Private mapData As Map : mapData.Initialize
    mapData.Put("msg", "Hi, I'm connected")
    wsh.SendEventToServer("RaisedByClient_Msg", mapData)
End Sub

This is the first msg sent from client to server (and you know how SendEventToServer works)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Mmmmm... I checked your code.
When you start the service SrvCommun you chect the wsh websocket. If it is not initialized od not connected you call a Sub Connect.

B4X:
Private Sub Connect
modUtils.LogSub("srvCommun", "Connect")
    wsh.Initialize(Me, "wsh")
    wsh.ws.Connect(Main.ServerLink)
End Sub

This sub performs wsh.ws.initialize (and set the callback) and then open the connection. But I think you shoud re-check the wsh.isInitialized status before the connect...

It can not be the problem: at first run wsh is surely not initiialized.

Have you tried the example?
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
No, I didn't try. I don't use b4j (give me the compiled JAR file and I'll try)

"At first run wsh is surely not initialized" is exacly what I mean.

Actually you check if .Isinizialized and if not you Initialize the wsh.
But IMMEDIATELY after this call you try to open the connection...
My iphotesis: Initalize is performed by the WebSocket library, in another thred and therefore it could be asynchronous.
You are trying to connect wsh.ws.Connect(Main.ServerLink) an object that is still not initialized. The pointer to this object is currently NULL and it throws the exception.
Try to insert some delay between Initialize and connect, something like "Loop until wsh.IsInizialized"
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
No, I didn't try. I don't use b4j (give me the compiled JAR file and I'll try)

"At first run wsh is surely not initialized" is exacly what I mean.

Actually you check if .Isinizialized and if not you Initialize the wsh.
But IMMEDIATELY after this call you try to open the connection...
My iphotesis: Initalize is performed by the WebSocket library, in another thred and therefore it could be asynchronous.
You are trying to connect wsh.ws.Connect(Main.ServerLink) an object that is still not initialized. The pointer to this object is currently NULL and it throws the exception.
Try to insert some delay between Initialize and connect, something like "Loop until wsh.IsInizialized"


Probably you are right; my wonderful response was due to my great big headache with which I woke up.

I'll try that loop ... but you should install B4J: it is light, it may be useful to you, and most importantly ...

you can help me (us) in this way (I'm so altruistic, hehehe)


Thanks, commandant Straker
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
The loop does not solve the problem and not even a pause of 3 seconds.

Ok, I'll install B4J in teh afternoon and I'll run then code.

- By the way, I'm not 'commandant' (at last Commander). I was Commander with Shado and now I'm retired. But you can call me 'Colonel':D
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Ok, I'll install B4J in teh afternoon and I'll run then code.

- By the way, I'm not 'commandant' (at last Commander). I was Commander with Shado and now I'm retired. But you can call me 'Colonel':D


I found Commandant & Commander are synonyms and Commandant is most used. Anyway, ok, i call you Captain (degraded, lol).



However, I have "found" the error due to the fact that I searched for the server jar... I have not found it, then I realized that I was using the debug mode. So, you helped me anyway.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Today I solved this problem, about 2.5 years after :D (I have not thought about this last 2.5 years :p).

Reading again this post:
Are you sending any message from the client immediately after the connection? If yes then try to use CallSubDelayed

and doing some tests...

When the websocket (client) event Connected is raised the server websocket may not be really ready to receive data.

So the right way is:

the client opens the connection (as usual) but the first data transmitted MUST be ALWAYS sent by the server!
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Today I solved this problem, about 2.5 years after :D (I have not thought about this last 2.5 years :p).

Reading again this post:


and doing some tests...

When the websocket (client) event Connected is raised the server websocket may not be really ready to receive data.

So the right way is:

the client opens the connection (as usual) but the first data transmitted MUST be ALWAYS sent by the server!


Thanks for the update, I encountered the same problem this morning, will try will your suggestion now.
 
Upvote 0
Top