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
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.
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?
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.
Are you creating a B4J server, Non-UI project, which creates 1000 clients that need to connect to another server ?
I don't think that's what you want to do, it seems rather strange to me.
Are you creating a B4J server, Non-UI project, which creates 1000 clients that need to connect to another server ?
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.
Resumable subs can only work when there is a message queue. By default, server handlers end when the Handle sub is completed. They do not create a message loop. If you want to wait for an event then you need to call StartMessageLoop and later StopMessageLoop. Example of handler that downloads...