Hi all, I am working on a server program to run on Windows and Linux using B4J. When the program receives a new connection I want to be able to raise an event so the event handler runs asynchronously from the new connection function. Something like this pseudo code.
The idea is to receive a connection. Raise an event for a function according to the type of connection then go back to listening for a new connection while the function runs.
I hope I was able to explain myself clearly enough.
Is there a method of creating a custom event and calling it? If so how and are there any examples I can view? I tried searching the forums and all I could find was CallSub but I don't know if it returns immediately and I couldn't get it to call a local function.
Even better if possible is to put each new connection into a separate background thread. Is that possible?
B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
Log("New Connection: ")
If Successful Then
Dim astream As AsyncStreams
astream.InitializePrefix(NewSocket.InputStream, False, NewSocket.OutputStream, "astream")
Select "controlcode from stream"
Case "code1"
Raise event For callCode1Function
Case "code2"
Raise event For callCode2Function
End Select
Else
Log(LastException)
End If
server.Listen
End Sub
The idea is to receive a connection. Raise an event for a function according to the type of connection then go back to listening for a new connection while the function runs.
I hope I was able to explain myself clearly enough.
Is there a method of creating a custom event and calling it? If so how and are there any examples I can view? I tried searching the forums and all I could find was CallSub but I don't know if it returns immediately and I couldn't get it to call a local function.
Even better if possible is to put each new connection into a separate background thread. Is that possible?