LoginHelper.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // 获取路由跟realmDispatcher地址
  15. RouterAddressComponent routerAddressComponent = clientScene.GetComponent<RouterAddressComponent>();
  16. if (routerAddressComponent == null)
  17. {
  18. routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ConstValue.RouterHttpHost, ConstValue.RouterHttpPort);
  19. await routerAddressComponent.Init();
  20. clientScene.AddComponent<NetClientComponent, AddressFamily>(routerAddressComponent.RouterManagerIPAddress.AddressFamily);
  21. }
  22. IPEndPoint realmAddress = routerAddressComponent.GetRealmAddress(account);
  23. R2C_Login r2CLogin;
  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. // 创建一个gate Session,并且保存到SessionComponent中
  29. Session gateSession = await RouterHelper.CreateRouterSession(clientScene, NetworkHelper.ToIPEndPoint(r2CLogin.Address));
  30. clientScene.AddComponent<SessionComponent>().Session = gateSession;
  31. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call(
  32. new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId});
  33. Log.Debug("登陆gate成功!");
  34. await EventSystem.Instance.PublishAsync(clientScene, new EventType.LoginFinish());
  35. }
  36. catch (Exception e)
  37. {
  38. await EventSystem.Instance.PublishAsync(clientScene, new EventType.LoginFinish() { exeception = e.Message });
  39. Log.Error(e);
  40. }
  41. }
  42. }
  43. }