Skip to content

Commit 5f9fe00

Browse files
authored
Merge pull request #136 from TaloDev/req-res-logging
Add request + response logging options
2 parents f594dac + ac7e174 commit 5f9fe00

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

Assets/Talo Game Services/Talo/Runtime/APIs/BaseAPI.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ protected async Task<string> Call(
6767

6868
var allHeaders = continuity ? headers : BuildHeaders();
6969

70+
if (Talo.Settings.logRequests && Debug.isDebugBuild)
71+
{
72+
Debug.Log($"<-- {method} {uri}{(continuity ? " [CONTINUITY]" : "")} {content}");
73+
}
74+
7075
if (Talo.Settings.offlineMode)
7176
{
7277
return HandleOfflineRequest(uri, method, content, allHeaders);
@@ -91,6 +96,11 @@ protected async Task<string> Call(
9196
await Task.Yield();
9297
}
9398

99+
if (Talo.Settings.logResponses && Debug.isDebugBuild)
100+
{
101+
Debug.Log($"--> {method} {uri} [{www.responseCode}] {www.downloadHandler.text}");
102+
}
103+
94104
if (www.result == UnityWebRequest.Result.Success)
95105
{
96106
return www.downloadHandler.text;
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using System;
12
using UnityEngine;
23

34
namespace TaloGameServices
45
{
5-
[System.Serializable]
6+
[Serializable]
67
public class SocketResponse
78
{
89
private class SocketMessage<T>
@@ -13,20 +14,28 @@ private class SocketMessage<T>
1314

1415
public string message;
1516

16-
public SocketResponse(string message)
17+
public SocketResponse(string message)
1718
{
1819
this.message = message;
1920
}
2021

2122
public string GetResponseType()
2223
{
23-
var message = JsonUtility.FromJson<SocketMessage<object>>(this.message);
24-
return message.res;
24+
var json = JsonUtility.FromJson<SocketMessage<object>>(message);
25+
return json.res;
2526
}
2627

2728
public T GetData<T>()
2829
{
2930
return JsonUtility.FromJson<SocketMessage<T>>(message).data;
3031
}
32+
33+
public object GetJsonData()
34+
{
35+
var json = message.Substring(1, message.Length - 2) // remove the curly braces
36+
.Replace("\"res\":\"" + GetResponseType() + "\",", "") // remove the response type
37+
.Replace("\"data\":", ""); // remove the data key, only keep the value
38+
return json;
39+
}
3140
}
3241
}

Assets/Talo Game Services/Talo/Runtime/TaloSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ public class TaloSettings : ScriptableObject
1616
public bool offlineMode = false;
1717
[Tooltip("Whether to automatically connect to the Talo socket when the game starts")]
1818
public bool autoConnectSocket = true;
19+
[Tooltip("Enable request logs in Debug builds")]
20+
public bool logRequests = true;
21+
[Tooltip("Enable response logs in Debug builds")]
22+
public bool logResponses = true;
1923
}
2024
}

Assets/Talo Game Services/Talo/Runtime/TaloSocket.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ private void HandleMessage(WebSocketConnection connection, WebSocketMessage wsm)
4343
var res = response.GetResponseType();
4444
OnMessageReceived?.Invoke(response);
4545

46+
if (Talo.Settings.logResponses && Debug.isDebugBuild)
47+
{
48+
Debug.Log($"--> WSS {res} {response.GetJsonData()}");
49+
}
50+
4651
switch (res)
4752
{
4853
case "v1.connected":
@@ -83,7 +88,13 @@ public void CloseConnection()
8388

8489
public void Send<T>(SocketRequest<T> request)
8590
{
86-
socket.AddOutgoingMessage(JsonUtility.ToJson(request));
91+
var data = JsonUtility.ToJson(request);
92+
93+
if (Talo.Settings.logRequests && Debug.isDebugBuild)
94+
{
95+
Debug.Log($"<-- WSS {request.req} {data}");
96+
}
97+
socket.AddOutgoingMessage(data);
8798
}
8899

89100
private void IdentifyPlayer()

0 commit comments

Comments
 (0)