B4J Question [WebsocketClientHandler] I'm trying to create 1000 websocket connections, but something isn't working...

Waldemar Lima

Well-Known Member
Licensed User
Hello guys, I'm trying to create 1000 websocket connections, but something isn't working...


B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    'Log("Hello world!!!")
    
    For i = 1 To 1000

        Dim wsh As WebSocketHandler
        wsh.Initialize(Me,"wsh")
        wsh.Connect("ws://127.0.0.1:80/mygame")
        Wait For wsh_Connected
        Log("connected "&i)
        Dim tmp As Map
        tmp.Initialize
        tmp.Put("RoomType", "1")
        tmp.Put("RoomSizeMax", "4")
        wsh.SendEventToServer("Device_FindMatch",tmp)
        'Sleep(1)

    Next

    StartMessageLoop
    
End Sub

Sub wsh_room_matchfound(params As List)
    Log("room found : ")
    Log(params)
    
End Sub

Sub wsh_Closed(reason As String)
    Log("disconnected.: "&reason)
End Sub

What am I doing wrong?
 

OliverA

Expert
Licensed User
Longtime User
Change to
B4X:
Sub AppStart (Args() As String)
    'Log("Hello world!!!")
    

    DoWork
    StartMessageLoop
    
End Sub

Sub DoWork
    For i = 1 To 1000

        Dim wsh As WebSocketHandler
        wsh.Initialize(Me,"wsh")
        wsh.Connect("ws://127.0.0.1:80/mygame")
        Wait For wsh_Connected
        Log("connected "&i)
        Dim tmp As Map
        tmp.Initialize
        tmp.Put("RoomType", "1")
        tmp.Put("RoomSizeMax", "4")
        wsh.SendEventToServer("Device_FindMatch",tmp)
        'Sleep(1)

    Next
End Sub
Otherwise, you exit the AppStart before you get to the StartMessageLoop.
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Thank you very much for your help ❤️, but I still don't understand the concept of this StartMessageLoop and StopMessageLoop.
The way described left me with more doubts... why is it necessary to place it inside a function and call StartMessageLoop after it?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
A Wait For exits the sub-routine in which it is placed. In your code, Wait For exits AppStart without ever getting to StartMessageLoop. In my code, Wait For exits DoWork and then resumes code in AppStart, executing the code following the DoWork code in AppStart and therefore executing the StartMessageLoop.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I think there's something wrong.

Are you creating a B4J server, Non-UI project, which creates 1000 clients that need to connect to another server :oops: ?
I don't think that's what you want to do, it seems rather strange to me.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I think there's something wrong.

Are you creating a B4J server, Non-UI project, which creates 1000 clients that need to connect to another server :oops: ?
I don't think that's what you want to do, it seems rather strange to me.
It makes sense, perhaps, if you just want to test how many clients can connect to a B4J websocket server, creating 1000 clients without a graphical interface.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but I still don't understand the concept of this StartMessageLoop and StopMessageLoop
 
Upvote 0
Top