B4J Question Websocket - messages sent order

LucaMs

Expert
Licensed User
Longtime User
It seems to me (I almost sure) that messages sent by a b4j server using a websocket can be delivered (or received by client) in a wrong order, sometimes.

Is this true?

(the server's log shows me that it has sent the messages in the right order, the client's log shows that they are arrived in the inverse order - only two messages. This happens sometimes).

SERVER LOG
INVIO EFFETTIVO A Phone (ArrayList) [[1, Phone, ...
INVIO EFFETTIVO A Phone (ArrayList) [[0, Tablet, ...
(INVIO EFFETTIVO A = EFFECTIVE DELIVERY TO. This log is set immediately before RunFunction)

CLIENT LOG
PlayerDataArrived
Nick name: Tablet Turn Order: 0

PlayerDataArrived
Nick name: Phone Turn Order: 1



Thank you
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Is this true?
No, it seems to be not so.

I did another test, because, while I set the server's log immediatley before the RunFunction, I set the client's log not in the right place or, better, the best place is in the ws_TextMessage of the websocket handler class.

So I discovered that the messages arrive in the right order. But what happen after?

ws_TextMessage calls a sub of its "parent" (callback) which is a service. This service uses a CallSubDelayed2 to call an Activity sub (this Activity is started by the CallSubDelayed2 command, in foreground there is another Activity).

Then? Is CallSubDelayed that sometimes "delivers its messages" in a wrong order?
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I did other tests with CallSubDelayed and services.

I don't understand the strange behavior of the project attached.

In it, if I use CallSubDelayed to call an Activity routine from a service, you can see that when you close the Activity it will be restarted, but without the routine is executed twice.
Also, "the focus" will return to the Main Activity.

If I use CallSubDelayed from Main directly, the behavior above does not happen.
 

Attachments

  • CallSubDelayed test.zip
    9.5 KB · Views: 171
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
if I use CallSubDelayed to call an Activity routine from a service, you can see that when you close the Activity it will be restarted,
This is due to the fast execution of the two CallSubDelayed; the first one starts the Activity, but the second one does not find the Activity already open, then it starts the Activity again.
I have to use:
B4X:
If IsPaused(actTwo) Then
    StartActivity(actTwo)
End If

Unfortunately, in the project attached that code is enough, in my app not :eek:

In fact it should not be sufficient because starting the Activity does not means wait that it has been completely started (resumed).

Any suggestion? (I tried to use a process globals boolean variable ActivityIsStarted, set it to True in Activity_Resume and add this code after StartActivity (in my app):
B4X:
If IsPaused(MyAct) Then
    StartActivity(MyAct)
    Do While Not(MyAct.ActivityIsStarted)
        DoEvents
    Loop
End If

(code "hated" by Erel :) ), but, although the Activity is called from a service, it seems that Activity_Resume (nor Activity_Create) is executed.

I could call a service's routine from Activity_Resume but it would be a mess.


Another day and I'm ready... for a psychiatrist! :confused::D
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
What is you use callSub, instead of CallSubDelayed?
Same problem: I check if the Activity is paused (although it is so for sure), then I start it but immediately after I call its routine, and at that moment the Activity "is not ready" (not resumed).

It is for this reason that usually I use CallSubDelayed, which starts (or brings in foreground) the Activity; but the problems is when you use two or more CallSubDelayed (needed).

Thank you, @jmon
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Unfortunately, in the project attached that code is enough, in my app not :eek:

In fact it should not be sufficient because starting the Activity does not means wait that it has been completely started (resumed).

:mad::mad::mad: I added a "big" loop in Activity_Create (to the project attached to #3) and I don't get errors, which means that the Activity is not started/resumed twice; but this still happens in my real project.

I know that to understand you would need my project, but I should post all, my server project too.

Is there (I fear "no") a way to know what starts/resumes an Activity (a kind of Intent? Perhaps I should ask this in the Wish forum). I don't see anywhere other calls to the Activity, then it seems to me that it is started again by the second CallSubDelayed, although this does not happens in the test project.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Tested my client, adding many Logs (thanks to my amazing AddLogs :p) and there are no other calls to that activity. This means that, for I don't knwo what reason, in my project the Activity is restarted by the second CallSubDelayed, but this not happens in the test project!!!

I don't know what else I can try!
 
Upvote 0
Top