본문으로 건너뛰기

서버 시간 조회

서버의 현재 시간을 조회하는 API입니다.

URL 확인

이 API는 api.playnanoo.com 도메인을 사용합니다.

API 정보​

  • URL: https://api.playnanoo.com/server_time/call
  • Method: POST
  • 인증 필요: 예
DeviceInfo 상속

이 API의 Req 클래스는 DeviceInfo를 상속받습니다. DeviceInfo의 모든 속성이 자동으로 포함됩니다.

요청 파라미터​

파라미터타입필수설명
udidstring필수디바이스 ID (분당 요청 제한용)

응답 데이터​

Res 클래스​

필드타입설명
codeint응답 코드
messagestring응답 메시지
valueServerTimeValue서버 시간 정보

ServerTimeValue 클래스​

필드타입설명
timezonestring타임존
timestampintUnix 타임스탬프
ISO_8601_datestringISO 8601 형식 날짜
datestring날짜 (YYYY-MM-DD)
yearstring연도
monthstring월
daystring일
hourstring시
minutestring분
secondstring초

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()
{
// 이벤트 비활성화 로직
}
}
서버 시간 활용

서버 시간은 게임 내 이벤트 시작/종료 시간 확인, 타임스탬프 검증 등에 활용할 수 있습니다. 클라이언트 시간 조작을 방지하기 위해 중요한 시간 체크는 항상 서버 시간을 기준으로 해야 합니다.

분당 요청 제한

디바이스 및 앱별로 분당 요청 횟수가 제한될 수 있습니다. 과도한 호출을 피하고 필요한 경우에만 서버 시간을 조회하세요.