LoginHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. R2C_Login r2CLogin;
  14. Session session = null;
  15. try
  16. {
  17. clientScene.RemoveComponent<RouterAddressComponent>();
  18. // 获取路由跟realmDispatcher地址
  19. RouterAddressComponent routerAddressComponent = clientScene.GetComponent<RouterAddressComponent>();
  20. if (routerAddressComponent == null)
  21. {
  22. routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ConstValue.RouterHttpHost, ConstValue.RouterHttpPort);
  23. await routerAddressComponent.Init();
  24. clientScene.AddComponent<NetKcpComponent, AddressFamily, int>(routerAddressComponent.RouterManagerIPAddress.AddressFamily, CallbackType.SessionStreamDispatcherClientOuter);
  25. }
  26. IPEndPoint realmAddress = routerAddressComponent.GetRealmAddress(account);
  27. session = await RouterHelper.CreateRouterSession(clientScene, realmAddress);
  28. {
  29. r2CLogin = (R2C_Login) await session.Call(new C2R_Login() { Account = account, Password = password });
  30. }
  31. }
  32. finally
  33. {
  34. session?.Dispose();
  35. }
  36. // 创建一个gate Session,并且保存到SessionComponent中
  37. Session gateSession = await RouterHelper.CreateRouterSession(clientScene, NetworkHelper.ToIPEndPoint(r2CLogin.Address));
  38. clientScene.AddComponent<SessionComponent>().Session = gateSession;
  39. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call(
  40. new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId});
  41. Log.Debug("登陆gate成功!");
  42. await Game.EventSystem.PublishAsync(clientScene, new EventType.LoginFinish());
  43. }
  44. catch (Exception e)
  45. {
  46. Log.Error(e);
  47. }
  48. }
  49. }
  50. }