B4J Question [WebApp] Chat Example: Question on ChatShared module usage

b4x-de

Active Member
Licensed User
Longtime User
I try to understand details on WebSockets in B4j by learning from examples provided by Erel. I have a question on the ChatExample that consists out of a ChatShared code module and two classes holding a WebSocket reference and using the code module (ChatLogin and Chat class).

My question is:

In the ChatLogin class the code module is accessed directly, i.e. in line 28:
ChatLogin class line 28:
ChatShared.AvoidDuplicates.Put(name.ToLowerCase, name)

But in the Chat class the code module is accessed by CallSubDelayed, i.e. in line 22:
Chat class line 22:
CallSubDelayed3(ChatShared, "NewConnection", Me, name)

What was the motivation to use different ways? Where are advantages of the one or the other way?

(Especially the Chat class surprised me, because long ago I learned that static code modules cannot handle events.)

Thank you very much,
Thomas
 

b4x-de

Active Member
Licensed User
Longtime User
Sorry, I posted in the wrong forum. This post should have appeared under questions.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
because long ago I learned that static code modules cannot handle events
That's true for B4A. In B4J and B4i code modules can handle events.

See this: https://www.b4x.com/android/forum/t...ads-sessions-and-server-events.39969/#content
Each request is handled by a different thread. By using CallSubDelayed we are causing the target sub to run on its "natural" thread. In the case of code modules it will be the main thread. In the case of server handler classes it will be the request thread.
 
Upvote 0
Top