GameNetManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameNetManager : MonoBehaviour {
  5. private static GameNetManager _Instance;
  6. public static GameNetManager Instance { get { return _Instance; } }
  7. public SocketMsgReceiveCenter
  8. mSocketMsgCenter;//这里主要做接收消息
  9. private SocketMsgSendManager mSocketMsgSendManager;//发消息的模块
  10. private void Awake()
  11. {
  12. _Instance = this;
  13. mSocketMsgCenter = new SocketMsgReceiveCenter
  14. ();
  15. mSocketMsgSendManager = new SocketMsgSendManager();
  16. }
  17. private void Test()
  18. {
  19. CDebug.Log("deviceModel " + SystemInfo.deviceModel);
  20. CDebug.Log("deviceName " + SystemInfo.deviceName);
  21. CDebug.Log("deviceType " + SystemInfo.deviceType.ToString());
  22. CDebug.Log(" deviceUniqueIdentifier " + SystemInfo.deviceUniqueIdentifier);
  23. CDebug.Log("platform " + Application.platform.ToString());
  24. }
  25. void Start () {
  26. //Test();
  27. //CheckNet();
  28. Application.targetFrameRate = 60;//默认60帧
  29. mSocketMsgCenter.Init();
  30. mSocketMsgSendManager.Init();
  31. }
  32. private void OnDestroy()
  33. {
  34. mSocketMsgCenter.Dispose();
  35. mSocketMsgSendManager.Dispose();
  36. }
  37. // Update is called once per frame
  38. void Update () {
  39. if(Input.GetKeyDown(KeyCode.B))
  40. {
  41. Engine.Net.NetWorkManager.GetInstance().OnDestroy();
  42. }
  43. }
  44. /// <summary>游戏刷帧</summary>
  45. void LateUpdate()
  46. {
  47. OnUpdate(CStaticMethod.SystemFrameTime());
  48. CheckSocketNet();
  49. }
  50. /// <summary>游戏刷帧</summary>
  51. void FixedUpdate()
  52. {
  53. OnFixedUpdate(CStaticMethod.SystemFrameTime());
  54. }
  55. /// <summary>渲染帧函数</summary>
  56. public void OnUpdate(int nTime)
  57. {
  58. Engine.Http.HttpManager.GetInstance().OnUpdate(nTime);
  59. GamePlayerData.Instance.teacher_operate_data.CheckPos();
  60. }
  61. /// <summary>帧函数</summary>
  62. public void OnFixedUpdate(int nTime)
  63. {
  64. //Http请求
  65. Engine.Http.HttpManager.GetInstance().OnFixedUpdate(nTime);
  66. Engine.Net.NetWorkManager.GetInstance().OnFixedUpdate(nTime);
  67. }
  68. //网络状态检测
  69. private void CheckNet()
  70. {
  71. switch (Application.internetReachability)
  72. {
  73. case NetworkReachability.NotReachable:
  74. CDebug.Log("网络断开");
  75. break;
  76. case NetworkReachability.ReachableViaLocalAreaNetwork:
  77. CDebug.Log("WIFI");
  78. break;
  79. case NetworkReachability.ReachableViaCarrierDataNetwork:
  80. CDebug.Log("4G/3G");
  81. break;
  82. }
  83. }
  84. //网络是否可用
  85. public bool NetIsAble
  86. {
  87. get { return Application.internetReachability != NetworkReachability.NotReachable; }
  88. }
  89. //只实现Socket连接监听 不管Http
  90. private float lastCheckTime;
  91. private float checkCD = 1;
  92. private float reconnectCD = 5;//尝试连接的CD
  93. private float lastConnectTime;
  94. private void CheckSocketNet()
  95. {
  96. if(Time.realtimeSinceStartup < lastCheckTime + checkCD)
  97. {
  98. return;
  99. }
  100. lastCheckTime = Time.realtimeSinceStartup;//记录上次检查的时间间隔
  101. if(!mSocketMsgCenter.IsWorking)
  102. {
  103. return;//还没有启动
  104. }
  105. if (!mSocketMsgCenter.IsConnecting || !NetIsAble)
  106. {
  107. //对于已经登陆的用户进行检查
  108. if (!mSocketMsgCenter.IsTrying && GamePlayerData.Instance.user_id != -1)
  109. {
  110. BadNet();
  111. }
  112. }
  113. if (NetIsAble)
  114. {
  115. if (mSocketMsgCenter.IsConnecting)
  116. {
  117. //CDebug.Log("网络恢复");
  118. GameNetManager.Instance.NetGood();//解除网络断开的提示
  119. }
  120. else if (!mSocketMsgCenter.IsTrying && GamePlayerData.Instance.user_id != -1)
  121. {
  122. if (Time.realtimeSinceStartup > lastConnectTime + reconnectCD)
  123. {
  124. lastConnectTime = Time.realtimeSinceStartup;
  125. mSocketMsgCenter.ReConnect();
  126. }
  127. }
  128. else if (GamePlayerData.Instance.user_id != -1 && !mSocketMsgCenter.IsConnecting)
  129. {
  130. //CDebug.Log("登陆界面的恢复");
  131. NetGood();
  132. }
  133. else
  134. {
  135. NetGood();
  136. }
  137. }
  138. else
  139. {
  140. //CDebug.Log("等待网络重新恢复-----");
  141. }
  142. }
  143. //网络断开的提示
  144. public void BadNet()
  145. {
  146. //这个是正常的网络断开 不需要重新登陆的
  147. // 如果当前登录面板是开启的状态 那么就不需要再弹提示框
  148. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_MSG_NOTICE, "网络断开");
  149. }
  150. public void NetGood()
  151. {
  152. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_MSG_NOTICE, "网络没有");
  153. }
  154. }