/// <summary> /// 登陆网关服 /// </summary> private static async ETTask <G2C_LoginGate> ConnectGate(Scene zoneScene, R2C_Login r2CLogin) { try { Log.Debug($"连接网关服 address:{r2CLogin.Address}"); // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address)); gateSession.AddComponent <SessionCallbackComponent>().DisposeCallback = GateSessionDispose; G2C_LoginGate response = await gateSession.Call(new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }) as G2C_LoginGate; gateSession.AddComponent <PingComponent>(); zoneScene.GetComponent <SessionComponent>().Session = gateSession; if (response == null) { throw new Exception($"登陆网关服失败 Error:response is null"); } if (!string.IsNullOrEmpty(response.Message)) { throw new Exception($"登陆网关服失败 Error:{response.Message}"); } Log.Debug($"连接网关服成功"); return(response); } catch (Exception e) { throw new Exception($"连接网关服异常 {e}"); } }
public static async ETVoid OnLoginAsync(Entity domain, string address, string account) { try { // 创建一个ETModel层的Session Session session = Game.Scene.GetComponent <NetOuterComponent>().Create(address); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = EntityFactory.Create <Session, Session>(domain, session); R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = account, Password = "******" }); realmSession.Dispose(); // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 Session gateSession = Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); Game.Scene.AddComponent <SessionComponent>().Session = gateSession; // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中 Game.Scene.AddComponent <SessionComponent>().Session = EntityFactory.Create <Session, Session>(Game.Scene, gateSession); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Info("登陆gate成功!"); // 创建Player Player player = EntityFactory.CreateWithId <Player>(Game.Scene, g2CLoginGate.PlayerId); PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.EventSystem.Run(EventIdType.LoginFinish); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }
/// <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}"); } }
public static async ETVoid OnLoginAsync() { try { UILoginComponent login = Game.Scene.GetComponent <UIComponent>().Get(UIType.UILogin).GetComponent <UILoginComponent>(); // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息 Session realmSession = Game.Scene.GetComponent <NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address); R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login() { Account = "abc", Password = "******" }); realmSession.Dispose(); Log.Info("正在登录中..."); //判断Realm服务器返回结果 if (r2CLogin.Error == ErrorCode.ERR_AccountOrPasswordError) { Log.Info("登录失败,账号或密码错误"); login.account.text = ""; login.password.text = ""; return; } // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中 Session gateSession = Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); Game.Scene.AddComponent <SessionComponent>().Session = gateSession; SessionComponent.Instance.Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); //判断登陆Gate服务器返回结果 if (g2CLoginGate.Error == ErrorCode.ERR_ConnectGateKeyError) { Log.Info("连接网关服务器超时"); login.account.text = ""; login.password.text = ""; gateSession.Dispose(); return; } //判断通过则登陆Gate成功 Log.Info("登陆gate成功!"); // 创建Player Player player = EntityFactory.CreateWithId <Player>(Game.Scene, g2CLoginGate.PlayerId); PlayerComponent playerComponent = Game.Scene.GetComponent <PlayerComponent>(); playerComponent.MyPlayer = player; Game.EventSystem.Run(EventIdType.LoginFinish); // 测试消息有成员是class类型 G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo)await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo()); } catch (Exception e) { Log.Error(e); } }