B4J Question Empty first servlet response from B4J server to client

Marvel

Active Member
Licensed User
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
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
 

Marvel

Active Member
Licensed User
I think that you are making things more complicated than they should be. Why does the server need to make http requests to jRDC2? Add these features to jRDC2 directly. jRDC2 = regular B4J server.
Hmmm.. I'll try that and give back with the result
 
Upvote 0

Marvel

Active Member
Licensed User
I think I'm missing something here. How do you query the SQL database from the B4j server itself without using dbrequest directly from the client.
I was thinking it will be safer to perform actions like checking if password is correct on the server and just returning a true or false to the client.

I really can't wrap my head around how this should work.?
 
Upvote 0

Marvel

Active Member
Licensed User
See the code in RDCHandler. It gets a SQL connection with con = Main.rdcConnector1.GetConnection. This is a SQL object. You can use it directly.

Make sure to close it when done. Even if there is an error.
Thanks. I eventually figured it out and used it directly the way you explained.
Thanks
 
Upvote 0
Top