//确认时间 public AliveModel TimeCheck(UserToken token, AliveModel model) { if (model == null || model.Time == 0) { return new AliveModel() { Statue = -1 } } ; if (!CacheProxy.User.IsOnline(token)) { //连接非法,玩家不在线 model.Statue = -2; } else { //连接合法 model.Statue = 0; //刷新时间 CacheProxy.Alive.RefreshTime(token); } model.Time = DateTime.Now.Ticks; return(model); }
public void MessageReceive(SocketModel model) { AliveModel am = model.GetMessage <AliveModel>(); if (am != null) { switch (am.Statue) { case 0: //让客户端的包时间和服务器保持一致 GameApp.Instance.AliveMgr.SeverTime = am.Time; break; case -1: //重新发送 GameApp.Instance.AliveMgr.LivingSelf(); break; case -2: //非法或离线 GameApp.Instance.CommonHintDlg.OpenHint("您已断开连接"); break; } } }
public void MessageReceive(UserToken token, SocketModel message) { switch (message.command) { case ALiveProtocol.Alive_Creq: AliveModel model = BizProxy.Alive.TimeCheck(token, message.GetMessage <AliveModel>()); token.write(TypeProtocol.Alive, ALiveProtocol.Alive_SRes, model); break; } }
/// <summary> /// 连接自救 /// </summary> public void LivingSelf() { //如果是第一次,那么新建一个包 if (AliveModel == null) { AliveModel = new AliveModel() { Statue = -1, } } ; //更新时间 AliveModel.Time = DateTime.Now.Ticks; //发送心跳包 this.Write(TypeProtocol.Alive, ALiveProtocol.Alive_Creq, AliveModel); } }
void StartAlive() { //如果是第一次,那么新建一个包 if (AliveModel == null) { AliveModel = new AliveModel() { Statue = -1, } } ; //更新时间 AliveModel.Time = DateTime.Now.Ticks; //发送心跳包 this.Write(TypeProtocol.Alive, ALiveProtocol.Alive_Creq, AliveModel); CheckTime(); GameApp.Instance.TimeMngr.AddSchedule(() => { //伪循环 StartAlive(); }, 3000); } /// <summary> /// 确认是否断连 /// </summary> void CheckTime() { if (AliveModel == null) { return; } if (AliveModel.Time - SeverTime > 30000) { GameApp.Instance.CommonHintDlg.OpenHint("您已断开连接"); } }