1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- namespace ET.Client
- {
- public static class LoginHelper
- {
- public static async ETTask Login(Scene clientScene, string account, string password)
- {
- try
- {
-
- clientScene.RemoveComponent<RouterAddressComponent>();
- clientScene.RemoveComponent<NetClientComponent>();
- clientScene.RemoveComponent<SessionComponent>();
-
-
- RouterAddressComponent routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ConstValue.RouterHttpHost, ConstValue.RouterHttpPort);
- await routerAddressComponent.Init();
-
- clientScene.AddComponent<NetClientComponent, AddressFamily>(routerAddressComponent.RouterManagerIPAddress.AddressFamily);
-
- IPEndPoint realmAddress = routerAddressComponent.GetRealmAddress(account);
-
-
- R2C_Login r2CLogin;
- using (Session session = clientScene.GetComponent<NetClientComponent>().Create(realmAddress))
-
- {
- r2CLogin = (R2C_Login) await session.Call(new C2R_Login() { Account = account, Password = password });
- }
- if (r2CLogin.Error != ErrorCode.ERR_Success)
- {
- Log.Error($"登陆验证错误...errCode={r2CLogin.Error}");
- return;
- }
-
- Session gameSession = clientScene.GetComponent<NetClientComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address));
-
-
-
- G2C_LoginGame g2CLoginGame = (G2C_LoginGame)await gameSession.Call(
- new C2G_LoginGame() { Key = r2CLogin.Key, GameId = r2CLogin.GameId});
- if (g2CLoginGame.Error != ErrorCode.ERR_Success)
- {
- Log.Error($"登陆game错误...errCode={g2CLoginGame.Error}");
- return;
- }
- if (ConstValue.SessionTimeoutTime > 0 && gameSession.GetComponent<SessionIdleCheckerComponent>() == null)
- {
- gameSession.AddComponent<SessionIdleCheckerComponent>();
- }
-
-
- clientScene.AddComponent<SessionComponent>().Session = gameSession;
-
- clientScene.GetComponent<RoleInfoComponment>().ClearRoleInfo();
- clientScene.GetComponent<RoleInfoComponment>().SetRoleInfo(g2CLoginGame);
- Log.Debug("登陆game成功!");
- await EventSystem.Instance.PublishAsync(clientScene, new EventType.LoginFinish());
- }
- catch (Exception e)
- {
- Log.Error($"登陆出错...{e.Message}");
- }
- }
- }
- }
|