/// <summary> /// 获得实例 /// </summary> /// <returns></returns> public static DNClient GetInst() { if (_instance == null) { _instance = new DNClient(); _instance.Name = "singleton client"; } return(_instance); }
/// <summary> /// 定时器函数 /// </summary> /// <param name="state"></param> private void OnTimerTick(object state) { DNClient client = DNClient.GetInstance(); if (Config.IsAutoHeartbeat && client.IsConnected) { float time = (DateTime.Now.Ticks - client.LastMsgSendTickTime) / 10000; if (time > Config.HeartBeatSendTime)//如果时间已经超过了那么就发送心跳包 { //发送一次心跳包 SendHeartBeat(); } } if (Config.IsAutoHeartbeat && client.IsConnected) { //如果15s没有收到心跳包 float time = (DateTime.Now.Ticks - client.LastMsgReceTickTime) / 10000; if (time > Config.HeartBeatCheckTime) { DxDebug.LogWarning("ClientTimer.OnTimerTick():长时间没有收到心跳包,判断可能已经掉线!"); client.Disconnect();//关闭连接 } } //执行事件 if (EventOnTimer != null) { try { EventOnTimer(); } catch (Exception e) { DxDebug.LogWarning("ClientTimer.OnTimerTick():执行EventOnTimer事件异常:" + e.Message); } } //执行3秒事件 if (EventOnTimer3S != null) { _count3S += KICK_TIME; if (_count3S >= 3 * 1000) //20秒 { _count3S = 0; try { EventOnTimer3S(); } catch (Exception e) { DxDebug.LogWarning("ClientTimer.OnTimerTick():执行EventOnTimer3S事件异常:" + e.Message);; } } } }
/// <summary> /// 服务器和客户端一起设置一个缓存文件夹 /// </summary> /// <param name="path">缓存文件夹路径</param> public static bool SetCacheDir(string path) { if (!DNClient.GetInstance().SetDirCache(path)) { return(false); } if (!DNServer.GetInstance().SetDirCache(path)) { return(false); } return(true); }
/// <summary> /// 发送心跳包函数,调用DNClient单例,然后发送。 /// </summary> public void SendHeartBeat() { DNClient client = DNClient.GetInstance(); if (client.IsConnected) { client.Send(Config.HeartBeatData); //发个心跳包 DxDebug.Log("ClientTimer:发送 HeartBeatData ~❤"); } else { // DxDebug.Log("ClientTimer:发送心跳包 - 但是当前还未连接"); } }