using Cal; using System; using System.Net; namespace ET { public class HeartBeatAwakeSystem : AwakeSystem { public override void Awake(HeartBeatComponent self) { self.Awake(); } } public class HeartBeatUpdateSystem : UpdateSystem { public override void Update(HeartBeatComponent self) { self.Update(); } } public class HeartBeatDestroySystem : DestroySystem { public override void Destroy(HeartBeatComponent self) { self.Destroy(); } } /// /// Session心跳组件(需要挂载到Session上) /// public class HeartBeatComponent : Entity { /// /// 更新间隔 /// private const int UpdateInterval = 1*1000; /// /// 超出时间 /// /// 如果跟客户端连接时间间隔大于在服务器上删除该Session private const int OutInterval = 5000; /// /// 记录时间 /// private long _recordDeltaTime = 0; /// /// 当前Session连接时间 /// public long CurrentTime = 0; private Session session; private int frame; public float DelayTime{get; private set;} internal void Awake() { session = this.GetParent(); } public void Update() { frame++; long now = TimeHelper.ClientNow(); // 如果没有到达发包时间、直接返回 if ((now - this._recordDeltaTime) < UpdateInterval || this.CurrentTime == 0) return; // 记录当前时间 this._recordDeltaTime = now; DelayTime = UpdateInterval / (float)frame; frame = 0; //Log.Warning($"FPS:{1000/delayTime:f2} time:{delayTime:f2}"); if (now - CurrentTime > OutInterval) { try { User user = session.GetComponent().User; if (user == null) { Log.Error($"user == null"); this.Dispose(); return; } } catch (Exception e) { Log.Error(e); } this.Dispose(); } else { //Log.Debug($"心跳成功:{session.GetComponent().User.NickName}"); } } internal void Destroy() { _recordDeltaTime = 0; CurrentTime = 0; frame = 0; } } }