查询服务器时间
用于查询服务器当前时间的 API。
URL 确认
此 API 使用 api.playnanoo.com 域名。
API 信息
- URL:
https://api.playnanoo.com/server_time/call - Method:
POST - 需要认证: 是
DeviceInfo 继承
此 API 的 Req 类继承自 DeviceInfo。DeviceInfo 的所有属性会自动包含。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| udid | string | 必填 | 设备 ID(用于每分钟请求限制) |
响应数据
Res 类
| 字段 | 类型 | 说明 |
|---|---|---|
| code | int | 响应代码 |
| message | string | 响应消息 |
| value | ServerTimeValue | 服务器时间信息 |
ServerTimeValue 类
| 字段 | 类型 | 说明 |
|---|---|---|
| timezone | string | 时区 |
| timestamp | int | Unix 时间戳 |
| ISO_8601_date | string | ISO 8601 格式日期 |
| date | string | 日期(YYYY-MM-DD) |
| year | string | 年 |
| month | string | 月 |
| day | string | 日 |
| hour | string | 时 |
| minute | string | 分 |
| second | string | 秒 |
Unity C# 实现
BaseResponse 类
所有 API 响应的基类。
public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}
字段说明:
ErrorCode:错误代码Message:错误信息WithdrawalKey:账号处于注销等待状态时,用于恢复的密钥(仅注销等待中的账号提供)BlockKey:账号被封禁时提供的密钥(仅被封禁的账号提供)
查询服务器时间类
using System;
using System.Collections;
using UnityEngine.Networking;
public class ServerTime
{
static string path = "https://api.playnanoo.com/server_time/call";
[Serializable]
public class Req : DeviceInfo
{
public string udid;
public IEnumerator Send(Action<Res> onSuccess, Action<BaseResponse> onError)
{
udid = device_id; // Device App별 분당 제한을 위하여 추가함.
yield return HttpClient.Send<Req, Res>(
UnityWebRequest.kHttpVerbPOST,
path,
requireToken: true,
body: this,
onSuccess: onSuccess,
onError: onError
);
}
}
[Serializable]
public class Res : BaseResponse
{
public int code;
public string message;
public ServerTimeValue value;
}
[Serializable]
public class ServerTimeValue
{
public string timezone;
public int timestamp;
public string ISO_8601_date;
public string date;
public string year;
public string month;
public string day;
public string hour;
public string minute;
public string second;
}
}
使用示例
基本用法
using PlayNANOO;
public class PlayNANOOExample : MonoBehaviour
{
void GetServerTime()
{
ServerTime.Req req = new ServerTime.Req();
StartCoroutine(req.Send(
onSuccess: res =>
{
Debug.Log($"Response Code: {res.code}");
Debug.Log($"Response Message: {res.message}");
Debug.Log($"Server Timestamp: {res.value.timestamp}");
Debug.Log($"Server Date: {res.value.date}");
Debug.Log($"Server Time: {res.value.hour}:{res.value.minute}:{res.value.second}");
Debug.Log($"ISO 8601: {res.value.ISO_8601_date}");
},
onError: (error) =>
{
Debug.LogError($"서버 시간 조회 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
客户端-服务器时间同步
using PlayNANOO;
using UnityEngine;
public class TimeSync : MonoBehaviour
{
private int serverTimestamp;
private float localTimeAtSync;
private bool isSynced = false;
void Start()
{
SyncWithServer();
}
public void SyncWithServer()
{
ServerTime.Req req = new ServerTime.Req();
StartCoroutine(req.Send(
onSuccess: res =>
{
serverTimestamp = res.value.timestamp;
localTimeAtSync = Time.realtimeSinceStartup;
isSynced = true;
Debug.Log($"시간 동기화 완료: {res.value.date} {res.value.hour}:{res.value.minute}:{res.value.second}");
},
onError: (error) =>
{
Debug.LogError($"시간 동기화 실패: {error.Message}");
}
));
}
public int GetCurrentServerTimestamp()
{
if (!isSynced)
{
Debug.LogWarning("서버 시간이 동기화되지 않았습니다.");
return 0;
}
float elapsedTime = Time.realtimeSinceStartup - localTimeAtSync;
return serverTimestamp + (int)elapsedTime;
}
public DateTime GetCurrentServerDateTime()
{
int currentTimestamp = GetCurrentServerTimestamp();
return DateTimeOffset.FromUnixTimeSeconds(currentTimestamp).DateTime;
}
}
活动时间检查
using PlayNANOO;
using UnityEngine;
using System;
public class EventTimeChecker : MonoBehaviour
{
private ServerTime.ServerTimeValue serverTime;
void CheckEventTime()
{
ServerTime.Req req = new ServerTime.Req();
StartCoroutine(req.Send(
onSuccess: res =>
{
serverTime = res.value;
// 이벤트 시작/종료 시간과 비교
int eventStartHour = 10; // 오전 10시
int eventEndHour = 22; // 오후 10시
int currentHour = int.Parse(serverTime.hour);
if (currentHour >= eventStartHour && currentHour < eventEndHour)
{
Debug.Log("이벤트 진행 중!");
EnableEvent();
}
else
{
Debug.Log("이벤트 시간이 아닙니다.");
DisableEvent();
}
},
onError: (error) =>
{
Debug.LogError($"서버 시간 조회 실패: {error.Message}");
}
));
}
void EnableEvent()
{
// 이벤트 활성화 로직
}
void DisableEvent()
{
// 이벤트 비활성화 로직
}
}
服务器时间用途
服务器时间可用于游戏内活动开始/结束时间确认、时间戳验证等。为防止客户端时间篡改,重要的时间检查应始终以服务器时间为准。
每分钟请求限制
每个设备和应用可能会限制每分钟的请求次数。请避免过度调用,仅在需要时查询服务器时间。