LoginHelper.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace ET.Client
  5. {
  6. public static class LoginHelper
  7. {
  8. public static async ETTask Login(Scene clientScene, string account, string password)
  9. {
  10. try
  11. {
  12. // 创建一个ETModel层的Session
  13. clientScene.RemoveComponent<RouterAddressComponent>();
  14. clientScene.RemoveComponent<NetClientComponent>();
  15. clientScene.RemoveComponent<SessionComponent>();
  16. // 获取路由跟realmDispatcher地址
  17. RouterAddressComponent routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ConstValue.RouterHttpHost, ConstValue.RouterHttpPort);
  18. await routerAddressComponent.Init();
  19. clientScene.AddComponent<NetClientComponent, AddressFamily>(routerAddressComponent.RouterManagerIPAddress.AddressFamily);
  20. IPEndPoint realmAddress = routerAddressComponent.GetRealmAddress(account);
  21. // 登录验证
  22. R2C_Login r2CLogin;
  23. using (Session session = clientScene.GetComponent<NetClientComponent>().Create(realmAddress))
  24. // using (Session session = await RouterHelper.CreateRouterSession(clientScene, realmAddress))
  25. {
  26. r2CLogin = (R2C_Login) await session.Call(new C2R_Login() { Account = account, Password = password });
  27. }
  28. if (r2CLogin.Error != ErrorCode.ERR_Success)
  29. {
  30. Log.Error($"登陆验证错误...errCode={r2CLogin.Error}");
  31. return;
  32. }
  33. // 创建一个game Session,并且保存到SessionComponent中
  34. Session gameSession = clientScene.GetComponent<NetClientComponent>().Create(NetworkHelper.ToIPEndPoint(r2CLogin.Address));
  35. // Session gameSession = await RouterHelper.CreateRouterSession(clientScene, NetworkHelper.ToIPEndPoint(r2CLogin.Address));
  36. // 登录game
  37. G2C_LoginGame g2CLoginGame = (G2C_LoginGame)await gameSession.Call(
  38. new C2G_LoginGame() { Key = r2CLogin.Key, GameId = r2CLogin.GameId});
  39. if (g2CLoginGame.Error != ErrorCode.ERR_Success)
  40. {
  41. Log.Error($"登陆game错误...errCode={g2CLoginGame.Error}");
  42. return;
  43. }
  44. if (ConstValue.SessionTimeoutTime > 0 && gameSession.GetComponent<SessionIdleCheckerComponent>() == null)
  45. {
  46. gameSession.AddComponent<SessionIdleCheckerComponent>();
  47. }
  48. // 保存账号信息
  49. clientScene.AddComponent<SessionComponent>().Session = gameSession;
  50. clientScene.GetComponent<RoleInfoComponment>().ClearRoleInfo();
  51. clientScene.GetComponent<RoleInfoComponment>().SetRoleInfo(g2CLoginGame);
  52. Log.Debug("登陆game成功!");
  53. await EventSystem.Instance.PublishAsync(clientScene, new EventType.LoginFinish());
  54. }
  55. catch (Exception e)
  56. {
  57. Log.Error($"登陆出错...{e.Message}");
  58. }
  59. }
  60. }
  61. }