Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Assets/Talo Game Services/Talo/Runtime/APIs/BaseAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected async Task<string> Call(
Debug.Log($"--> {method} {uri} [{www.responseCode}] {www.downloadHandler.text}");
}

await Talo.Continuity.HandlePostResponseHealthCheck(uri.ToString(), www.result);

if (www.result == UnityWebRequest.Result.Success)
{
return www.downloadHandler.text;
Expand Down
43 changes: 40 additions & 3 deletions Assets/Talo Game Services/Talo/Runtime/APIs/HealthCheckAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,60 @@

namespace TaloGameServices
{
public enum HealthCheckStatus
{
OK,
FAILED,
UNKNOWN
}

public class HealthCheckAPI : BaseAPI
{
private HealthCheckStatus lastHealthCheckStatus = HealthCheckStatus.UNKNOWN;

public HealthCheckAPI() : base("v1/health-check") { }

public HealthCheckStatus GetLastStatus()
{
return lastHealthCheckStatus;
}

public async Task<bool> Ping()
{
var uri = new Uri(baseUrl);

bool success;
try
{
await Call(uri, "GET");
return true;
} catch
success = true;
}
catch
{
Debug.LogWarning("Health check failed");
return false;
success = false;
}

bool failedLastHealthCheck = lastHealthCheckStatus == HealthCheckStatus.FAILED;

if (success)
{
lastHealthCheckStatus = HealthCheckStatus.OK;
if (failedLastHealthCheck)
{
Talo.InvokeConnectionRestored();
}
}
else
{
lastHealthCheckStatus = HealthCheckStatus.FAILED;
if (!failedLastHealthCheck)
{
Talo.InvokeConnectionLost();
}
}

return success;
}
}
}
38 changes: 37 additions & 1 deletion Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ public class PlayersAPI : BaseAPI

private readonly string offlineDataPath = Application.persistentDataPath + "/ta.bin";

public PlayersAPI() : base("v1/players") { }
public PlayersAPI() : base("v1/players")
{
Talo.OnConnectionRestored += OnConnectionRestored;
}

private async void OnConnectionRestored()
{
await Talo.Socket.ResetConnection();

if (Talo.HasIdentity())
{
var socketToken = await CreateSocketToken();
if (!string.IsNullOrEmpty(socketToken))
{
Talo.Socket.SetSocketToken(socketToken);
}
}
}

public void InvokeIdentifiedEvent()
{
Expand Down Expand Up @@ -202,5 +219,24 @@ public async Task<PlayersSearchResponse> Search(string query)
var res = JsonUtility.FromJson<PlayersSearchResponse>(json);
return res;
}

public async Task<string> CreateSocketToken()
{
Talo.IdentityCheck();

var uri = new Uri($"{baseUrl}/socket-token");

try
{
var json = await Call(uri, "POST");
var res = JsonUtility.FromJson<PlayersSocketTokenResponse>(json);
return res.socketToken;
}
catch (Exception ex)
{
Debug.LogWarning($"Failed to create socket token: {ex.Message}");
return "";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TaloGameServices
{
[System.Serializable]
public class PlayersSocketTokenResponse
{
public string socketToken;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/Talo Game Services/Talo/Runtime/Talo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ namespace TaloGameServices
{
public class Talo
{
public static event Action OnConnectionLost;
public static event Action OnConnectionRestored;

private static bool _testMode;

internal static bool TestMode => _testMode;

internal static void InvokeConnectionLost()
{
OnConnectionLost?.Invoke();
}

internal static void InvokeConnectionRestored()
{
OnConnectionRestored?.Invoke();
}

internal static EventsAPI _events;
internal static PlayersAPI _players;
internal static LeaderboardsAPI _leaderboards;
Expand Down Expand Up @@ -218,5 +231,15 @@ internal static bool CheckTestMode()
_testMode = false;
return _testMode;
}

internal static void HandleConnectionLost()
{
OnConnectionLost?.Invoke();
}

internal static void HandleConnectionRestored()
{
OnConnectionRestored?.Invoke();
}
}
}
5 changes: 0 additions & 5 deletions Assets/Talo Game Services/Talo/Runtime/TaloSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ public void SetSocketToken(string token)

public async Task ResetConnection()
{
if (!identified)
{
return;
}

CloseConnection();
socketAuthenticated = false;
identified = false;
Expand Down
30 changes: 30 additions & 0 deletions Assets/Talo Game Services/Talo/Runtime/Utils/ContinuityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

namespace TaloGameServices
{
Expand Down Expand Up @@ -122,5 +124,33 @@ public void ClearRequests()
_requests.Clear();
WriteRequests();
}

public async Task HandlePostResponseHealthCheck(string url, UnityWebRequest.Result result)
{
if (url.Contains("/health-check"))
{
return;
}

var isConnectionError = result == UnityWebRequest.Result.ConnectionError;
var isDataProcessingError = result == UnityWebRequest.Result.DataProcessingError;

if (result == UnityWebRequest.Result.Success)
{
// if offline mode is enabled, check if it shouldn't be
if (Talo.HealthCheck.GetLastStatus() == HealthCheckStatus.FAILED)
{
await Talo.HealthCheck.Ping();
}
}
else if (isConnectionError || isDataProcessingError)
{
// if offline mode isn't enabled, check if it should be
if (Talo.HealthCheck.GetLastStatus() != HealthCheckStatus.FAILED)
{
await Talo.HealthCheck.Ping();
}
}
}
}
}