B4J Question HTTP - Release or Disconnect

Declan

Well-Known Member
Licensed User
Longtime User
I have the following code that listens on a port and receives data from the Sigfox cloud via a callback.
The data is parsed, then I need to close or release the HTTP connection in order for Sigfox to acknowledge that the callback has been received.

B4X:
Sub AStream_NewData (Buffer() As Byte)
   
    Dim MyStr As String
    MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
   
    MyStr = MyStr.Trim
   
    Dim strJSON As String
    Dim pos1 As Int
    Dim pos2 As Int
   
    pos1 = MyStr.IndexOf("{")
    pos2 = MyStr.LastIndexOf("}")
    strJSON=MyStr.SubString2(pos1,(pos2 + 1))

    Dim parser As JSONParser
    parser.Initialize(strJSON)
    Dim root As Map = parser.NextObject
    Dim data As String = root.Get("data")
    Dim device As String = root.Get("device")
    Log(data)
    Log(device)
   
    ' Close Connection
    Disconnect
   
End Sub

My Disconnect:
B4X:
Private Sub Disconnect
    CloseExistingConnection
End Sub

It seems that "Disconnect" is not correct.
How do I successfully disconnect or release the HPPT connection?
 
Last edited:

Declan

Well-Known Member
Licensed User
Longtime User
Just something I tried:
B4X:
Sub CloseExistingConnection
    If astream.IsInitialized Then astream.Close
    If client.IsInitialized Then client.Close
    UpdateState (False)
End Sub
I also tried:
astream.Close
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Based on the other thread about the json, I understand that the other device sends a http request. This means that it expects a http response.

This code is from B4J OAuth class:
B4X:
Private Sub PrepareServer
    If serversock.IsInitialized Then serversock.Close
    If astream.IsInitialized Then astream.Close
    Do While True
        Try
            serversock.Initialize(port, "server")
            serversock.Listen
            Exit
        Catch
            port = port + 1
            Log(LastException)
        End Try
    Loop
    Wait For server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
        Dim Response As StringBuilder
        Response.Initialize
        Do While Response.ToString.Contains("Host:") = False
            Wait For AStream_NewData (Buffer() As Byte)
            Response.Append(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
        Loop
        astream.Write(("HTTP/1.0 200" & Chr(13) & Chr(10)).GetBytes("UTF8"))
        Sleep(50)
        astream.Close
        serversock.Close
        ParseBrowserUrl(Regex.Split2("$",Regex.MULTILINE, Response.ToString)(0))
    End If
    
End Sub
It reads the client request and returns a simple response.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Happy Christmas
I have tried:
B4X:
Private Sub ListenForConnections
    Do While working
        server.Listen
        Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
        If Successful Then
            'CloseExistingConnection
            client = NewSocket
            astream.Initialize(client.InputStream,  client.OutputStream, "astream")
            UpdateState(True)
            '--------------------------------------
            Dim Response As StringBuilder
            Response.Initialize
            Do While Response.ToString.Contains("Host:") = False
                Wait For AStream_NewData (Buffer() As Byte)
                Response.Append(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
            Loop
            astream.Write(("HTTP/1.0 200" & Chr(13) & Chr(10)).GetBytes("UTF8"))
            Sleep(50)
            astream.Close
            server.Close
            CloseExistingConnection
            '--------------------------------------
        End If
    Loop
End Sub
But I do not get to "Sub AStream_NewData (Buffer() As Byte)"
 
Last edited:
Upvote 0
Top