概述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using com.mile.common.message;
using System;
using UIFramework;
public class HearteatManager : Singleton<HearteatManager>, I_OnUpdate,I_OnRelease
{
/// <summary>
/// 当前同步的服务器时间
/// </summary>
public long CurServerTime
{
get
{
return (CurDateTime - curGetTime) + curServerTime;
}
}
/// <summary>
/// 平均游戏延迟
/// </summary>
public long AverageGamePing
{
get
{
return averageDelayTime / heartBeatCount;
}
}
/// <summary>
/// 当前游戏延迟
/// </summary>
public long CurGamePing
{
get
{
return curDelayTime;
}
}
/// <summary>
/// 当前时间戳
/// </summary>
/// <returns></returns>
public string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
/// <summary>
/// 获取当前的时间 的毫秒数
/// </summary>
/// <returns></returns>
public long CurDateTime
{
get
{
return DateTime.Now.Ticks / 10000;
}
}
/// <summary>
/// 游戏启动时的时间
/// </summary>
private long startTime = 0;
#region 心跳参数
/// <summary>
/// 心跳间隔
/// </summary>
public int HeartBeatSpace = 5;
private int heartBeatCount = 0;
private bool isHeartBeat = false;
private long curServerTime = 0;
/// <summary>
/// 当前发送心跳时间
/// </summary>
private long curSendTime = -1;
/// <summary>
/// 下次发送心跳时间
/// </summary>
private long nextSendTime = -1;
/// <summary>
/// 当前心跳返回时间
/// </summary>
private long curGetTime;
/// <summary>
/// 平均延迟时间总和
/// </summary>
/// <returns></returns>
private long averageDelayTime = 0;
private long curDelayTime = 0;
#endregion
public HearteatManager()
{
startTime = CurDateTime;
}
/// <summary>
/// 开始心跳
/// </summary>
public void StartHeartBeat()
{
FacadeGlobal.Log.Log("开始执行心跳");
InitHeartBeat();
isHeartBeat = true;
}
public void StopHeartBeat()
{
isHeartBeat = false;
}
/// <summary>
/// 初始化 心跳
/// </summary>
private void InitHeartBeat()
{
curSendTime = -1;
averageDelayTime = 0;
heartBeatCount = 0;
curDelayTime = 0;
curGetTime = CurDateTime;
}
private void UpdateHeartBeat()
{
if (!isHeartBeat) return;
//开始心跳立即执行一次
if (curSendTime == -1)
{
HeartBeat();
}
if (nextSendTime < CurDateTime)
{
HeartBeat();
}
}
/// <summary>
/// 同步时间执行方法
/// </summary>
public void HeartBeat()
{
FacadeGlobal.NetWorkManager.Send(MessageID.CGheartbeatClientSend);
curSendTime = CurDateTime;
nextSendTime = CurDateTime + HeartBeatSpace * 1000;
}
/// <summary>
/// 服务器心跳回调
/// </summary>
/// <param name="msg"></param>
public void OnHeartBeatBack(GCheartbeatServerBack msg)
{
//当前返回的服务器时间
long curBackServerTime = msg.ServerTime;
curGetTime = CurDateTime;
heartBeatCount++;
curDelayTime = (curGetTime - curSendTime) / 2;
averageDelayTime += curDelayTime;
this.curServerTime = curBackServerTime + AverageGamePing;
//FacadeGlobal.Log.Log("平均延迟 : " + AverageGamePing);
//FacadeGlobal.Log.Log("当前延迟 : " + CurGamePing);
//FacadeGlobal.Log.Log("收到心跳回调 服务器时间 curBackServerTime: " + curBackServerTime);
//FacadeGlobal.Log.Log("收到心跳回调 服务器时间 curServerTime: " + curServerTime);
}
public void OnUpdate()
{
UpdateHeartBeat();
TimeOutUpdate();
}
private void TimeOutUpdate()
{
//return;
if (!isHeartBeat) return;
if (curSendTime - curGetTime < 10000) //心跳超时 10秒
return;
isHeartBeat = false;
if(FacadeGlobal.NetWorkManager.serverIsConnected)
FacadeGlobal.UIManager.OpenMutexUI(false, EnumUIType.theNetWork);
}
public void OnRelease()
{
isHeartBeat = false;
InitHeartBeat();
}
}
PS:本人认为不该用机器的系统时间 该用游戏真正开始时候开始计时 那样做
最后
以上就是壮观日记本为你收集整理的心跳的全部内容,希望文章能够帮你解决心跳所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复