@@ -12,6 +12,7 @@ namespace GodotModules.Netcode
1212 public class WebClient : IDisposable
1313 {
1414 public static HttpClient Client { get ; set ; }
15+ public static bool ConnectionAlive { get ; set ; }
1516 public static Task < WebServerResponse < LobbyListing [ ] > > TaskGetServers { get ; set ; }
1617 public static string ExternalIp { get ; set ; }
1718 private static int FailedPingAttempts { get ; set ; }
@@ -31,6 +32,16 @@ public WebClient()
3132 TimerPingMasterServer = new ( WebClient . WEB_PING_INTERVAL ) ;
3233 TimerPingMasterServer . AutoReset = true ;
3334 TimerPingMasterServer . Elapsed += new ( OnTimerPingMasterServerEvent ) ;
35+
36+ try
37+ {
38+ ExternalIp = new System . Net . WebClient ( ) . DownloadString ( "http://icanhazip.com" ) . Replace ( "\\ r\\ n" , "" ) . Replace ( "\\ n" , "" ) . Trim ( ) ;
39+ }
40+ catch ( Exception e )
41+ {
42+ ExternalIp = "" ;
43+ System . Console . WriteLine ( $ "Failed to get external IP { e . Message } \n { e . StackTrace } ") ;
44+ }
3445 }
3546
3647 public async static Task < WebServerResponse < string > > Post ( string path , Dictionary < string , string > values )
@@ -61,6 +72,20 @@ public async static Task<WebServerResponse<string>> Post(string path, Dictionary
6172 }
6273 }
6374
75+ public static async Task UpdateIsAlive ( )
76+ {
77+ try
78+ {
79+ var response = await Client . GetAsync ( $ "http://{ WEB_SERVER_IP } /api/ping") ;
80+ await response . Content . ReadAsStringAsync ( ) ;
81+ ConnectionAlive = true ;
82+ }
83+ catch ( Exception )
84+ {
85+ ConnectionAlive = false ;
86+ }
87+ }
88+
6489 public static async Task < WebServerResponse < T > > Get < T > ( string path )
6590 {
6691 try
@@ -91,7 +116,7 @@ public static async Task<WebServerResponse<T>> Get<T>(string path)
91116
92117 public async void OnTimerPingMasterServerEvent ( System . Object source , ElapsedEventArgs args )
93118 {
94- var res = await WebClient . Post ( "ping" , new Dictionary < string , string > { { "Name" , SceneLobby . CurrentLobby . Name } } ) ;
119+ var res = await WebClient . Post ( "servers/ ping" , new Dictionary < string , string > { { "Name" , SceneLobby . CurrentLobby . Name } } ) ;
95120 if ( res . Status == WebServerStatus . ERROR )
96121 {
97122 FailedPingAttempts ++ ;
@@ -104,12 +129,6 @@ public async void OnTimerPingMasterServerEvent(System.Object source, ElapsedEven
104129 FailedPingAttempts = 0 ; // reset failed ping attempts if a ping gets through
105130 }
106131
107- public static void GetExternalIp ( )
108- {
109- string externalIpString = new System . Net . WebClient ( ) . DownloadString ( "http://icanhazip.com" ) . Replace ( "\\ r\\ n" , "" ) . Replace ( "\\ n" , "" ) . Trim ( ) ;
110- ExternalIp = IPAddress . Parse ( externalIpString ) . ToString ( ) ;
111- }
112-
113132 public void Dispose ( )
114133 {
115134 TimerPingMasterServer . Dispose ( ) ;
0 commit comments