/// <summary> /// 结束链接 /// </summary> private static void EndConnect(Scene zoneScene, bool isConnect) { Log.Debug("结束登陆"); LoginComponent loginState = zoneScene.GetComponent <LoginComponent>(); if (isConnect) { loginState.NetworkState = E_NetworkState.Connect; loginState.ReconnectionCount = 0; loginState.IsAutoLogin = true;//登陆成功后设置为可以自动登陆 } //关闭菊花 //UIHelper.HideWaiting(); }
/// <summary> /// 返回登陆场景 /// </summary> public static async ETVoid BackLogin(Scene zoneScene) { Log.Debug("当前用户选择返回登陆场景"); LoginComponent loginState = zoneScene.GetComponent <LoginComponent>(); //返回登陆设置为不自动登陆 loginState.IsAutoLogin = false; //重置当前连接 ResetConnect(zoneScene); //TODO:切换到登陆场景 await Game.EventSystem.Publish(new EventType.ChangeScene() { Scene = zoneScene, SceneName = "Init" }); }
/// <summary> /// 自动登陆 /// </summary> public static void AutoLogin(Scene zoneScene) { Log.Debug("当前尝试自动连接服务器"); //获取当前登陆数据 LoginComponent loginState = zoneScene.GetComponent <LoginComponent>(); if (loginState.CanReconnect) { Connect(zoneScene, loginState.Address, loginState.LoginData, ConnectSuccess, ConnectFailure).Coroutine(); } else { Log.Debug($"当前不能自动登陆 autoLogin:{loginState.IsAutoLogin} address:{loginState.Address} loginData:{loginState.LoginData}"); } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="zoneScene">scene</param> /// <param name="address">服务器地址</param> /// <param name="loginData">登陆数据</param> /// <param name="success">成功回调</param> /// <param name="failure">失败回调</param> /// <returns></returns> private static async ETVoid Connect(Scene zoneScene, string address, string loginData, Action <Scene, G2C_LoginGate> success, Action <Scene> failure) { try { LoginComponent loginState = zoneScene.GetComponent <LoginComponent>(); if (!loginState.CanConnect) { Log.Error("当前正在连接或已经连接不需要再次连接"); return; } //开始连接 StartConnect(zoneScene); Log.Debug($"开始登陆 address:{address} loginData:{loginData}"); //登陆验证服 R2C_Login r2cLogin = await ConnectRealm(zoneScene, address, loginData); //登陆网关服 G2C_LoginGate g2cLoginGate = await ConnectGate(zoneScene, r2cLogin); //登陆成功 success.Invoke(zoneScene, g2cLoginGate); //保存登陆信息供重连使用 loginState.Address = address; loginState.LoginData = loginData; //结束连接 EndConnect(zoneScene, true); } catch (Exception e) { //登陆异常 failure.Invoke(zoneScene); //失败重置 ResetConnect(zoneScene); //结束连接 EndConnect(zoneScene, false); Log.Error($"登陆异常 {e}"); } }
/// <summary> /// 重连失败 /// </summary> private static async void ReconnectFailure(Scene zoneScene) { LoginComponent loginState = zoneScene.GetComponent <LoginComponent>(); Log.Debug($"重连失败 Count:{loginState.ReconnectionCount}"); //断线自动重连15次,15次后提示是否重连 if (loginState.ReconnectionCount < 15) { await TimerComponent.Instance.WaitAsync(1000); if (loginState.CanConnect) { loginState.ReconnectionCount++; Log.Debug($"第{loginState.ReconnectionCount}次自动重连"); Reconnect(zoneScene); } } else { loginState.ReconnectionCount = 0; //弹窗提示是否重连 TryConnect(zoneScene, "多次重连失败, 是否继续重连!"); } }