1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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
- {
- // 创建一个ETModel层的Session
- clientScene.RemoveComponent<RouterAddressComponent>();
- // 获取路由跟realmDispatcher地址
- RouterAddressComponent routerAddressComponent = clientScene.GetComponent<RouterAddressComponent>();
- if (routerAddressComponent == null)
- {
- 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 = await RouterHelper.CreateRouterSession(clientScene, realmAddress))
- {
- r2CLogin = (R2C_Login) await session.Call(new C2R_Login() { Account = account, Password = password });
- }
- // 创建一个gate Session,并且保存到SessionComponent中
- Session gateSession = await RouterHelper.CreateRouterSession(clientScene, NetworkHelper.ToIPEndPoint(r2CLogin.Address));
- clientScene.AddComponent<SessionComponent>().Session = gateSession;
-
- G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call(
- new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId});
- Log.Debug("登陆gate成功!");
- await EventSystem.Instance.PublishAsync(clientScene, new EventType.LoginFinish());
- }
- catch (Exception e)
- {
- await EventSystem.Instance.PublishAsync(clientScene, new EventType.LoginFinish() { exeception = e.Message });
- Log.Error(e);
- }
- }
- }
- }
|