public static async ETVoid Login(Scene zoneScene, string address, string account) { try { // 创建一个ETModel层的Session R2C_Login r2CLogin; using (Session session = zoneScene.GetComponent <NetOuterComponent>().Create(address)) { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = "******" }); } // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = Game.Scene.GetComponent <NetOuterComponent>().Create(r2CLogin.Address); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Info("登陆gate成功!"); await Game.EventSystem.Publish(new EventType.LoginFinish()); } catch (Exception e) { Log.Error(e); } }
/// <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 ETTask Login(Scene zoneScene, string address, string account, string password, Action onError = null) { try { // 创建一个ETModel层的Session R2C_Login r2CLogin; Session session = null; try { session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address)); { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = password }); } } finally { session?.Dispose(); } long channelId = RandomHelper.RandInt64(); var routercomponent = zoneScene.AddComponent <GetRouterComponent, long, long>(r2CLogin.GateId, channelId); string routerAddress = await routercomponent.Tcs; if (routerAddress == "") { zoneScene.RemoveComponent <GetRouterComponent>(); throw new Exception("routerAddress 失败"); } Log.Debug("routerAddress 获取成功:" + routerAddress); zoneScene.RemoveComponent <GetRouterComponent>(); // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(channelId, NetworkHelper.ToIPEndPoint(routerAddress)); gateSession.AddComponent <RouterDataComponent>().Gateid = r2CLogin.GateId; gateSession.AddComponent <PingComponent>(); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Debug("登陆gate成功!"); await Game.EventSystem.PublishAsync(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { onError?.Invoke(); Log.Error(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); } }
public static async ETTask Login(Scene zoneScene, string address, string account, string password) { try { // 创建一个ETModel层的Session R2C_Login r2CLogin; Session session = null; try { session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address)); { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = password }); } } finally { session?.Dispose(); } // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address)); gateSession.AddComponent <PingComponent>(); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Debug("登陆gate成功!"); await Game.EventSystem.Publish(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { Log.Error(e); } }
public static async ETVoid Login(Scene zoneScene, string address, string account, string password) { try { // 创建一个ETModel层的Session R2C_Login r2CLogin; using (Session session = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address))) { r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = password }); } if (r2CLogin.Error != ErrorCode.ERR_Success) { Log.Info("登陆失败!"); return; } // 创建一个gate Session,并且保存到SessionComponent中 Session gateSession = zoneScene.GetComponent <NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address)); gateSession.AddComponent <PingComponent>(); zoneScene.AddComponent <SessionComponent>().Session = gateSession; G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call( new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId }); Log.Info("登陆gate成功!"); await Game.EventSystem.Publish(new EventType.LoginFinish() { ZoneScene = zoneScene }); } 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}"); } }
/// <summary> /// 链接成功 /// </summary> private static async void ConnectSuccess(Scene zoneScene, G2C_LoginGate g2CLoginGate) { try { Log.Debug($"登陆成功"); // ETModel.SDK.SdkManager.JpushSetAlias(g2CLoginGate.User.UserId.ToString()); // #region Test // //保存用户信息 创建Player // // Player player = ComponentFactory.CreateWithId<Player>(g2CLoginGate.User.UserId); // // PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>(); // // playerComponent.MyPlayer = player; // #endregion await Game.EventSystem.Publish(new EventType.LoginFinish() { ZoneScene = zoneScene }); } catch (Exception e) { throw new Exception($"登录成功切换场景异常 重置到登录场景 {e}"); } }
/// <summary> /// 重连成功 /// </summary> private static void ReconnectSuccess(Scene zoneScene, G2C_LoginGate g2CLoginGate) { Log.Debug($"重连成功"); // BaseModel.SetSelf(g2CLoginGate.User); }
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); } }