Skip to main content

Interface IDataSendService

A service responsible for queuing and sending data, typically in JSON format, to an external system or server.

public interface IDataSendService : IUnityGameService

Implements​

IUnityGameService

Examples​

Implementing a minimal alternative sender and swapping it in place of DataSender.

DataSender self-registers in its Awake via TryRegister, so to replace it with a custom implementation use ForceRegister after the scene has loaded, or place the custom MonoBehaviour in the bootstrap scene so its Awake runs first.

// Custom sender — logs to the Unity console instead of posting to the server:
public class ConsoleSender : MonoBehaviour, IDataSendService
{
private void Awake()
{
// ForceRegister replaces any existing IDataSendService (e.g. DataSender)
// without logging an error, making it safe for test/debug scenes.
UnityServiceLocator.Current.ForceRegister(this);
}

private void OnDestroy()
=> UnityServiceLocator.Current.TryUnregister(this);

// EnqueueData must be non-blocking; start a coroutine for any async work.
public void EnqueueData(string jsonName, string jsonData, Action callback)
{
StartCoroutine(SendRoutine(jsonName, jsonData, callback));
}

private IEnumerator SendRoutine(string jsonName, string jsonData, Action callback)
{
UnityEngine.Debug.Log($"[ConsoleSender] {jsonName}: {jsonData}");
yield return null; // yield at least once, matching the IRequest contract
callback?.Invoke();
}
}

// Any analysis handler that calls EnqueueData is unaware of the swap:
if (UnityServiceLocator.Current.TryGet(out var sender))
{
sender.EnqueueData("survey_p42", surveyJson, () => Debug.Log("sent"));
}

Methods​

EnqueueData(string, string, Action)​

Enqueues a data package for sending.

void EnqueueData(string jsonName, string jsonData, Action callback)

Parameters​

NameTypeDescription
jsonNamestringThe identifier or label for the JSON payload.
jsonDatastringThe actual JSON string to be sent.
callbackActionA callback to be invoked after the data is sent.