B4J Question Send data to WebSocket from another Sub

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I have been looking at the Login System filters tutorial (https://www.b4x.com/android/forum/threads/server-login-system-filters-tutorial.39020/#content) and have a question with it.

In my Main Module I have:
B4X:
srvr.Initialize("srvr")
    srvr.Port = 8888
    srvr.AddWebSocket("/ws", "OnlineSocket")
    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
    srvr.LogsFileFolder = File.Combine(File.DirApp, "logs")
    srvr.AddFilter("/login_example/members/*", "MembersFilter", False)

    Dim err As Map
        err.Initialize
        err.Put(404, "/404.html") 'page not found
        err.Put(500, "/500.html") 'server error
    srvr.SetCustomErrorPages(err)
    srvr.Start

I then have a Class called 'OnlineSocket' and I am logging when a user connects etc.

B4X:
'WebSocket class
Sub Class_Globals
    Private ws As WebSocket 'ignore
End Sub

Public Sub Initialize
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    Log("Connected")
    ws = WebSocket1
End Sub

Private Sub WebSocket_Disconnected
    Log("Disconnected")
End Sub

I am also using the jNetwork library. This will listen for TCP & UDP messages coming from a Telnet device. (ServerSocket)

What I am trying to do is detect the incoming TCP/UDP message (which I have working) and only send that incoming message to some of the logged in user as a WebSocket.

For Example:
User 1 (User)
User 2 (User)
User 3 (Admin)
User 4 (User)

I only want the incoming messages from the TCP/UDP connection to be sent as a WebSocket message to the Admin (user 3) and user 1, 2 & 4 shouldn't see this WebSocket message.

Any ideas on how I can send a WebSocket message to only specific users from a different module (from the incoming TCP/UDP Module) ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
You can create a map with userId and the websocket of that user.
How do I send the data?

I am starting the WebSocket using:
B4X:
srvr.Initialize("srvr")
    srvr.Port = 8888
    srvr.AddWebSocket("/ws", "OnlineSocket")
    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
    srvr.LogsFileFolder = File.Combine(File.DirApp, "logs")
    srvr.AddFilter("/login_example/members/*", "MembersFilter", False)

    Dim err As Map
        err.Initialize
        err.Put(404, "/404.html") 'page not found
        err.Put(500, "/500.html") 'server error
    srvr.SetCustomErrorPages(err)
    srvr.Start

How do I access that WebSocket?

Should I use something like:
OnlineSocket.ws.RunFunction("JavaScriptFunction",Null)

and then change
B4X:
Private ws As WebSocket 'ignore
to
B4X:
Dim ws As WebSocket 'ignore
in the OnlineSocket Module ?
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I apologize for reading hastily; maybe I did not understand the question and, moreover, I do not remember how filter works.

I think you should create a class (or at least a Type) for each user.
This class will contain a constant for the type of user, a reference to the relative websocket handler class [object] (you have a handler class which receives a websocket) and other stuff (user name, ...).
Then, a mapUsers of clsUser.

[CreateThreadSafeMap]
 
Last edited:
Upvote 0
Top