Exemplo n.º 1
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());
 }
Exemplo n.º 2
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;
            }
        }