B4J Question is it okay to use httpjob within my tcp server ?

Addo

Well-Known Member
Licensed User
Longtime User
i made a tcp server that works so far so good is it okay to use httpjob inside it safely ?

as example

B4X:
Sub Class_Globals
    
    Private server As ServerSocket
    Private clients As List
    Public cuniqueid As Int
    Private ClientTimer As Timer
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(port As Int)
Log("Server Initialized")

cuniqueid = 10
server.Initialize(port, "server")
clients.Initialize
ClientTimer.Initialize("ConnectionCheck",2000)
ClientTimer.Enabled = True

server.Listen
    
End Sub


public Sub doHttpNotfiy(aurl As String, adata As String)
    
    
    Dim j As HttpJob
    Dim urlok As String


    urlok = aurl
    j.Initialize("", Me)
    j.PostString(urlok, adata)

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
    Log("requested")
    End If
    j.Release

    
End Sub


Private Sub ConnectionCheck_tick

If clients.Size > 0 Then

For i =  clients.Size -1 To 0 Step -1
    
Dim aClient As TcClient
Dim ctimenow As Long
Dim afloortime As Double
aClient = clients.Get(i)
ctimenow = DateTime.Now
afloortime = Floor((ctimenow - aClient.iamlive) / DateTime.TicksPerSecond)

If (afloortime >= 75) And (aClient.iamconnected = "YES")  Then
aClient.iamconnected = "NO"
Log("Remove Dead Connection "&aClient.userid)
aClient.diconnectme
Exit
End If


Next
    
    
        
End If

End Sub

Private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim c As TcClient
c.Initialize(NewSocket, Me)
c.userip = NewSocket.RemoteAddress
c.iamlive = DateTime.Now
c.iamconnected = "YES"
c.userid = cuniqueid
cuniqueid = cuniqueid + 1
Log("New Connection "&c.userip& " id "&cuniqueid)
clients.Add(c)
End If

server.Listen


End Sub


Public Sub getMyIp As String
Return server.GetMyIP
End Sub

Public Sub getNumberOfClients As Int
Return clients.Size
End Sub


Public Sub ClientDisconnected (aclient As TcClient)
Dim i As Int = clients.IndexOf(aclient)
    
If i > -1 Then
aclient.iamconnected = "NO"
clients.RemoveAt(i)
End If

End Sub


Public Sub SendTome(aclient As TcClient, data As String)
    
aclient.SendData(data)

End Sub


Public Sub SendCommandToAll(data As String)
If clients.Size > 0 Then
    
For i =  clients.Size -1 To 0 Step -1

Dim aClient As TcClient
aClient = clients.Get(i)

If (aClient.iamconnected = "YES") Then
aClient.SendData(data)
End If

Next
            
End If
    

End Sub


can i use doHttpNotfiy sub safely like that ?


B4X:
Public Sub NewData (data As String)
Dim paramset() As String
Dim paramcount As Int
Dim Command As String

paramset = Regex.Split("\|", data)
paramcount = paramset.Length

If paramcount > 0 Then

Command = paramset(0)

End If


If Command = "SET" Then
 
If paramcount = 2 Then
Aserver.doHttpNotfiy("http://someurl", "somedata")
End If


End If

If Command = "isalive" Then
updatelivetime
End If

If Command = "close" Then
CallSub(Main, "CloseTheserver")
End If

If Command = "Login" Then

If paramcount = 2 Then
ConnectionTime = DateTime.Time(DateTime.Now)
Aserver.SendTome(Me, "logged")
End If

End If

If Command = "msg" Then
 
If paramcount = 2 Then
Aserver.SendCommandToAll("msg|")
End If


End If

If Command = "PING" Then
 
If paramcount = 2 Then
Aserver.SendCommandToAll("PONG|")
End If


End If







End Sub
 
Top