B4J Question [Server] Dealing with concurrent clients ?

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I look at the demo (server)code of the websockets an I see the WebSocket_Connected method.
I assume that each concurrent clientconnection gets its own WebSocket instance server-side ?

When I receive a message from the client (Device_Message method in the example) it seems this method does not have a websocket as parameter. How do I know from which socket the request is comming from ?

I would need to have a link between a websocket and a user identification, and I guess this is only possible when the client sends a message to the server with the user identification as a parameter, the Device_Message method could then link the ID with the websocket instance...

I also noticed there is a TextMessage event on the WebSocket class: should I use this event ?

Thanks

B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    timer1.Initialize("timer1", 1000)
    timer1.Enabled = True
End Sub

Sub Timer1_Tick
    ws.RunFunction("ServerTime", Array As Object(DateTime.Time(DateTime.Now)))
    ws.Flush
End Sub

Sub Device_Message(Params As Map)
    Log("Device message: " & Params.Get("message"))
End Sub

Private Sub WebSocket_Disconnected
    timer1.Enabled = False
    Log("disconnected")
End Sub
 
Last edited:

billzhan

Active Member
Licensed User
Longtime User
I assume that each concurrent client connection gets its own WebSocket instance server-side ?

Yes, each one has an unique session object to the client(ws.Session) in debug and release mode. Session object is changing every time even when the same client connects.

When I receive a message from the client (Device_Message method in the example) it seems this method does not have a websocket as parameter. How do I know from which socket the request is comming from ?
The client could send an unique username or device id to the server.

See the web socket chat example, after enter an unique username in login page, the participator are redirected to chatroom, the username is shown in chat.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I also noticed there is a TextMessage event on the WebSocket class: should I use this event ?
Where did you see it? There is no such event.

I would need to have a link between a websocket and a user identification, and I guess this is only possible when the client sends a message to the server with the user identification as a parameter, the Device_Message method could then link the ID with the websocket instance
This is actually quite simple. Each session (connection) is tied to a specific class instance.
The user id is stored in a global variable.

Note that the push example is a more complicated example. I recommend you to start with simpler examples.
 
Last edited:
Upvote 0

MaFu

Well-Known Member
Licensed User
Longtime User
Hi,

I look at the demo (server)code of the websockets an I see the WebSocket_Connected method.
...
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    timer1.Initialize("timer1", 1000)
    timer1.Enabled = True
End Sub

Sub Timer1_Tick
    ws.RunFunction("ServerTime", Array As Object(DateTime.Time(DateTime.Now)))
    ws.Flush
End Sub

Sub Device_Message(Params As Map)
    Log("Device message: " & Params.Get("message"))
End Sub

Private Sub WebSocket_Disconnected
    timer1.Enabled = False
    Log("disconnected")
End Sub
The code above is in the file B4A.bas which is a class file. For every new socket connection a new B4A class instance is created.
And on "WebSocket_Connected()" the WebSocket is saved in the variable "ws". Therefore on "Device_Message()" you have the sending WebSocket already, it's in "ws".
 
Upvote 0
Top