예제 #1
0
        public async Task <Client> Connect()
        {
            if (state == PlayState.CONNECTING)
            {
                //
                Logger.Debug("it is connecting...");
                return(null);
            }
            if (state != PlayState.INIT && state != PlayState.DISCONNECT)
            {
                throw new PlayException(PlayExceptionCode.StateError,
                                        string.Format("You cannot call Connect() on {0} state", state.ToString()));
            }
            try {
                state     = PlayState.CONNECTING;
                lobbyConn = await ConnectLobby();

                state = PlayState.LOBBY;
                Logger.Debug("connected at: {0}", Thread.CurrentThread.ManagedThreadId);
                lobbyConn.OnMessage += OnLobbyConnMessage;
                lobbyConn.OnClose   += OnLobbyConnClose;
                return(this);
            } catch (Exception e) {
                state = PlayState.INIT;
                throw e;
            }
        }
예제 #2
0
 void GameToLobby(LobbyConnection lc)
 {
     state = PlayState.LOBBY;
     gameConn.OnMessage -= OnGameConnMessage;
     gameConn.OnClose   -= OnGameConnClose;
     gameConn.Close();
     Logger.Debug("connected at: {0}", Thread.CurrentThread.ManagedThreadId);
     lobbyConn            = lc;
     lobbyConn.OnMessage += OnLobbyConnMessage;
     lobbyConn.OnClose   += OnLobbyConnClose;
 }
예제 #3
0
 Task <LobbyConnection> ConnectLobby()
 {
     return(playRouter.Fetch().OnSuccess(t => {
         var serverUrl = t.Result;
         Logger.Debug("play server: {0} at {1}", serverUrl, Thread.CurrentThread.ManagedThreadId);
         return lobbyRouter.Fetch(serverUrl);
     }).Unwrap().OnSuccess(t => {
         var lobbyUrl = t.Result;
         Logger.Debug("wss server: {0} at {1}", lobbyUrl, Thread.CurrentThread.ManagedThreadId);
         return LobbyConnection.Connect(AppId, lobbyUrl, UserId, GameVersion);
     }).Unwrap());
 }
예제 #4
0
        public Client(string appId, string appKey, string userId, bool ssl = true, string gameVersion = "0.0.1", string playServer = null)
        {
            AppId       = appId;
            AppKey      = appKey;
            UserId      = userId;
            Ssl         = ssl;
            GameVersion = gameVersion;

            state = PlayState.INIT;
            Logger.Debug("start at {0}", Thread.CurrentThread.ManagedThreadId);

            var playGO = new GameObject("LeanCloud.Play");

            UnityEngine.Object.DontDestroyOnLoad(playGO);
            context = playGO.AddComponent <PlayContext>();

            playRouter  = new PlayRouter(appId, playServer);
            lobbyRouter = new LobbyRouter(appId, false, null);
            lobbyConn   = new LobbyConnection();
            gameConn    = new GameConnection();
        }
예제 #5
0
        public async Task LeaveRoom()
        {
            if (state != PlayState.GAME)
            {
                throw new PlayException(PlayExceptionCode.StateError,
                                        string.Format("You cannot call LeaveRoom() on {0} state", state.ToString()));
            }
            state = PlayState.GAME_TO_LOBBY;
            try {
                await gameConn.LeaveRoom();
            } catch (Exception e) {
                state = PlayState.GAME;
                throw e;
            }
            try {
                lobbyConn = await ConnectLobby();

                GameToLobby(lobbyConn);
            } catch (Exception e) {
                state = PlayState.INIT;
                throw e;
            }
        }
예제 #6
0
        internal async Task Join()
        {
            if (state == State.Joining || state == State.Lobby)
            {
                return;
            }
            state = State.Joining;
            LobbyInfo lobbyInfo;

            try {
                lobbyInfo = await Client.lobbyService.Authorize();
            } catch (Exception e) {
                state = State.Init;
                throw e;
            }
            try {
                lobbyConn = new LobbyConnection();
                await lobbyConn.Connect(Client.AppId, lobbyInfo.Url, Client.GameVersion, Client.UserId, lobbyInfo.SessionToken);

                await lobbyConn.JoinLobby();

                lobbyConn.OnMessage = (cmd, op, body) => {
                    switch (cmd)
                    {
                    case CommandType.Lobby:
                        switch (op)
                        {
                        case OpType.RoomList:
                            HandleRoomListUpdated(body.RoomList);
                            break;

                        default:
                            Logger.Error("unknown msg: {0}/{1} {2}", cmd, op, body);
                            break;
                        }
                        break;

                    case CommandType.Statistic:
                        break;

                    case CommandType.Error: {
                        Logger.Error("error msg: {0}", body);
                        ErrorInfo errorInfo = body.Error.ErrorInfo;
                        Client.OnError?.Invoke(errorInfo.ReasonCode, errorInfo.Detail);
                    }
                    break;

                    default:
                        Logger.Error("unknown msg: {0}/{1} {2}", cmd, op, body);
                        break;
                    }
                };
                state = State.Lobby;
            } catch (Exception e) {
                if (lobbyConn != null)
                {
                    await lobbyConn.Close();
                }
                state = State.Init;
                throw e;
            }
        }