I've been trying my hands on B4j server recently using jrdc2 but I've run into a problem along the way. Basically, I want to use servletresponse.write to send a reply to the B4a client. The problem is that the response queue is wrong. For the first response, it sends an empty string, for the second response, it sends what it should have sent at first, so the response I get in the B4a client is always an outdated response.(Note: this is the behaviour when I do not use startmessageloop in the handle to wait for the subs)
When I use startmessageloop I keep getting this error
To give context, the use case of the code below is for the B4j server to register new users and check if user details exists then give a response to the B4a client on the status (e.g user exists or successfully registered)
This is a sample code
When I use startmessageloop I keep getting this error
B4X:
ResponseError. Reason: java.net.SocketTimeoutException: timeout, Response:
To give context, the use case of the code below is for the B4j server to register new users and check if user details exists then give a response to the B4a client on the status (e.g user exists or successfully registered)
This is a sample code
B4X:
Sub Class_Globals
Private ResponseTOclient As ServletResponse
End Sub
Public Sub Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
ResponseTOclient = resp
Dim data_map As Map = req.GetMultipartData(Null, 100)
For Each key As String In data_map.Keys
Select key
Case "username"
Dim p As Part = data_map.Get("username")
Dim username As String = p.GetValue("utf8")
Case "password"
Dim p As Part = data_map.Get("password")
Dim password As String = p.GetValue("utf8")
End Select
Next
checkexisting(username, password)
startmessageloop
End Sub
Sub checkexisting(username As String, password As String)
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("check_existing_user", Array(email))
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
req.HandleJobAsync(j, "req")
Wait For(req) req_Result(Res As DBResult)
Dim row() As Object = Res.Rows.Get(0)
Dim count As Int = row(0)
If count > 0 Then
ResponseTOclient.Write("Email already registered") 'In situations where this is the first response, I get an empty string
stopmessageloop
Else
RegisterUser(username, password)
End If
End If
j.Release
End Sub
Sub RegisterUser(username As String, password As String)
Dim cmd As DBCommand = CreateCommand("register_user", Array(username, password))
Dim j As HttpJob = CreateRequest.ExecuteBatch(Array(cmd), Null)
Wait For(j) JobDone(j As HttpJob)
If j.Success Then
ResponseTOclient.Write("User successfully registered")
stopmessageloop
End If
j.Release
End Sub