ServerTime API
ServerTime API
用于周期奖励(每日/每周奖励、活动期间等)判定的 tudada 服务器时间(epoch ms),代替设备时间。设备时间可被用户修改,因此奖励判定请使用服务器时间。
使用分两步。
- 进入时同步一次 —
GetServerTime()。首次成功后,应用从后台返回时 SDK 会自动重新同步。 - 判定时即时读取 —
GetServerTimeSync()。不请求服务器,返回由上次同步值推算出的时间。若从未同步成功则返回null,此时不要发放奖励。
提示
进入奖励界面时再调用一次 GetServerTime(),在没有恢复信号的环境(审核、构建测试)中也能保持安全。
GetServerTime(onSuccess, onFail)
查询服务器时间。失败时请暂缓发放。
参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
onSuccess | Action<ServerTimeResult> | - | 成功回调 |
onFail | Action<ServerTimeFailResult> | - | 失败回调 |
成功响应 (ServerTimeResult):
| 字段 | 类型 | 说明 |
|---|---|---|
now | long | 服务器时间(整数 epoch ms,无时区) |
errMsg | string | 结果消息 |
失败响应 (ServerTimeFailResult):
| 字段 | 类型 | 说明 |
|---|---|---|
reason | string | 失败原因(见下表) |
errMsg | string | 错误消息 |
reason | 含义 | 处理 |
|---|---|---|
network | 网络错误 | 稍后重试 |
timeout | 响应超时 | 稍后重试 |
bad_response | 响应格式不符 | 稍后重试 |
unsupported | 当前运行环境不提供服务器时间 | 5 分钟内立即失败,之后由下一次 GetServerTime() 调用(若已同步成功过,从后台返回时的自动重新同步也会)重新尝试。期间暂缓发放。若 WebGL 模板仍加载旧版 JS SDK 包(低于 0.3.0),在更换包之前会一直是 unsupported |
hidden | 因切换到后台而丢弃了请求,或在页面隐藏期间调用 | 回到前台后再次调用 |
TudadaSDK.Instance.GetServerTime(
onSuccess: (result) => Debug.Log("服务器时间: " + result.now),
onFail: (result) => Debug.Log("查询失败: " + result.reason)
);
GetServerTimeSync()
立即返回由上次同步值推算出的服务器时间,不请求服务器。
返回值 (ServerTimeSync,同步前为 null):
| 字段 | 类型 | 说明 |
|---|---|---|
now | long | 服务器时间(整数 epoch ms)。应用挂起期间可能落后于实际时间,但不会超前 |
stale | bool | 从后台返回后重新同步尚未成功时为 true |
var t = TudadaSDK.Instance.GetServerTimeSync();
if (t != null && t.now >= nextRewardAt)
{
GrantDailyReward(); // 即使 t.stale 也安全 — 值不会超前
}
备注
"不会超前"的保证不包含服务器实例之间的时钟偏差(NTP 误差)。本 API 不保证时钟的绝对精确。
Unity Editor
在编辑器中,GetServerTime() 以设备 UTC 时间响应,之后 GetServerTimeSync() 也返回设备 UTC 时间(调用前为 null)。