123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GameNetManager : MonoBehaviour {
- private static GameNetManager _Instance;
- public static GameNetManager Instance { get { return _Instance; } }
- public SocketMsgReceiveCenter
- mSocketMsgCenter;//这里主要做接收消息
- private SocketMsgSendManager mSocketMsgSendManager;//发消息的模块
- private void Awake()
- {
- _Instance = this;
- mSocketMsgCenter = new SocketMsgReceiveCenter
- ();
- mSocketMsgSendManager = new SocketMsgSendManager();
- }
- private void Test()
- {
- CDebug.Log("deviceModel " + SystemInfo.deviceModel);
- CDebug.Log("deviceName " + SystemInfo.deviceName);
- CDebug.Log("deviceType " + SystemInfo.deviceType.ToString());
- CDebug.Log(" deviceUniqueIdentifier " + SystemInfo.deviceUniqueIdentifier);
- CDebug.Log("platform " + Application.platform.ToString());
- }
- void Start () {
- //Test();
- //CheckNet();
- Application.targetFrameRate = 60;//默认60帧
- mSocketMsgCenter.Init();
- mSocketMsgSendManager.Init();
- }
- private void OnDestroy()
- {
- mSocketMsgCenter.Dispose();
- mSocketMsgSendManager.Dispose();
- }
- // Update is called once per frame
- void Update () {
- if(Input.GetKeyDown(KeyCode.B))
- {
- Engine.Net.NetWorkManager.GetInstance().OnDestroy();
- }
- }
- /// <summary>游戏刷帧</summary>
- void LateUpdate()
- {
- OnUpdate(CStaticMethod.SystemFrameTime());
- CheckSocketNet();
- }
- /// <summary>游戏刷帧</summary>
- void FixedUpdate()
- {
- OnFixedUpdate(CStaticMethod.SystemFrameTime());
- }
- /// <summary>渲染帧函数</summary>
- public void OnUpdate(int nTime)
- {
- Engine.Http.HttpManager.GetInstance().OnUpdate(nTime);
- GamePlayerData.Instance.teacher_operate_data.CheckPos();
- }
- /// <summary>帧函数</summary>
- public void OnFixedUpdate(int nTime)
- {
- //Http请求
- Engine.Http.HttpManager.GetInstance().OnFixedUpdate(nTime);
- Engine.Net.NetWorkManager.GetInstance().OnFixedUpdate(nTime);
- }
- //网络状态检测
- private void CheckNet()
- {
- switch (Application.internetReachability)
- {
- case NetworkReachability.NotReachable:
- CDebug.Log("网络断开");
- break;
- case NetworkReachability.ReachableViaLocalAreaNetwork:
- CDebug.Log("WIFI");
- break;
- case NetworkReachability.ReachableViaCarrierDataNetwork:
- CDebug.Log("4G/3G");
- break;
- }
- }
- //网络是否可用
- public bool NetIsAble
- {
- get { return Application.internetReachability != NetworkReachability.NotReachable; }
- }
- //只实现Socket连接监听 不管Http
- private float lastCheckTime;
- private float checkCD = 1;
- private float reconnectCD = 5;//尝试连接的CD
- private float lastConnectTime;
- private void CheckSocketNet()
- {
- if(Time.realtimeSinceStartup < lastCheckTime + checkCD)
- {
- return;
- }
- lastCheckTime = Time.realtimeSinceStartup;//记录上次检查的时间间隔
- if(!mSocketMsgCenter.IsWorking)
- {
- return;//还没有启动
- }
- if (!mSocketMsgCenter.IsConnecting || !NetIsAble)
- {
- //对于已经登陆的用户进行检查
- if (!mSocketMsgCenter.IsTrying && GamePlayerData.Instance.user_id != -1)
- {
- BadNet();
- }
- }
- if (NetIsAble)
- {
- if (mSocketMsgCenter.IsConnecting)
- {
- //CDebug.Log("网络恢复");
- GameNetManager.Instance.NetGood();//解除网络断开的提示
- }
- else if (!mSocketMsgCenter.IsTrying && GamePlayerData.Instance.user_id != -1)
- {
- if (Time.realtimeSinceStartup > lastConnectTime + reconnectCD)
- {
- lastConnectTime = Time.realtimeSinceStartup;
- mSocketMsgCenter.ReConnect();
- }
- }
- else if (GamePlayerData.Instance.user_id != -1 && !mSocketMsgCenter.IsConnecting)
- {
- //CDebug.Log("登陆界面的恢复");
- NetGood();
- }
- else
- {
- NetGood();
- }
- }
- else
- {
- //CDebug.Log("等待网络重新恢复-----");
- }
- }
- //网络断开的提示
- public void BadNet()
- {
- //这个是正常的网络断开 不需要重新登陆的
- // 如果当前登录面板是开启的状态 那么就不需要再弹提示框
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_MSG_NOTICE, "网络断开");
- }
- public void NetGood()
- {
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_MSG_NOTICE, "网络没有");
- }
- }
|