LoginHelper.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. namespace ET.Client
  3. {
  4. public static class LoginHelper
  5. {
  6. public static async ETTask Login(Scene clientScene, string account, string password)
  7. {
  8. try
  9. {
  10. // 创建一个ETModel层的Session
  11. R2C_Login r2CLogin;
  12. Session session = null;
  13. try
  14. {
  15. clientScene.RemoveComponent<RouterAddressComponent>();
  16. // 获取路由跟realmDispatcher地址
  17. RouterAddressComponent routerAddressComponent = clientScene.GetComponent<RouterAddressComponent>();
  18. if (routerAddressComponent == null)
  19. {
  20. routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string>(ConstValue.RouterHttpAddress);
  21. await routerAddressComponent.Init();
  22. }
  23. string realmAddress = routerAddressComponent.GetRealmAddress(account);
  24. session = await RouterHelper.CreateRouterSession(clientScene, realmAddress);
  25. {
  26. r2CLogin = (R2C_Login) await session.Call(new C2R_Login() { Account = account, Password = password });
  27. }
  28. }
  29. finally
  30. {
  31. session?.Dispose();
  32. }
  33. // 创建一个gate Session,并且保存到SessionComponent中
  34. Session gateSession = await RouterHelper.CreateRouterSession(clientScene, r2CLogin.Address);
  35. clientScene.AddComponent<SessionComponent>().Session = gateSession;
  36. G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call(
  37. new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId});
  38. Log.Debug("登陆gate成功!");
  39. await Game.EventSystem.PublishAsync(clientScene, new EventType.LoginFinish());
  40. }
  41. catch (Exception e)
  42. {
  43. Log.Error(e);
  44. }
  45. }
  46. }
  47. }