Hi. I've added network client connection within my service. While starting service, socket connects successfully. But if connection breaks, or server not available, I can't reconnect without restarting service. Only first "_Connected" event has "Success".
B4X:
Sub Service_Create
Sock.Initialize("Sock")
End Sub
Sub Service_Start (StartingIntent As Intent)
If NetMode>1 Then
Sock.Connect(TCPServer, TCPPort,20000)
End If
End Sub
Sub Sock_Connected(Success As Boolean)
If Success Then
Try
AstNetIsOpen=True
astNet.Initialize(Me,"ASTNet",Sock.InputStream, Sock.OutputStream)
Catch
ToastMessageShow("Can't open network stream",False)
AstNetIsOpen=False
AstNetTimeout=30
End Try
If AstNetIsOpen Then
ToastMessageShow("Network connected.", False)
astNet.Write("::0," & DevMac)
End If
Else
If astNet.IsInitialized Then astNet.Close
Sock.Close
AstNetIsOpen=False
AstNetTimeout=30
ToastMessageShow("Connecting error.", False)
End If
End Sub
Sub ElTimerCmd_Tick
If NetMode>1 And AstNetIsOpen=False Then
If AstNetTimeout>0 Then
AstNetTimeout=AstNetTimeout-1
Else
Sock.Connect(TCPServer, TCPPort,20000)
End If
End If
End Sub