예제 #1
0
        private IEnumerator HandleRequestFacebookLoginRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestFacebookLoginMessage message = messageHandler.ReadMessage <RequestFacebookLoginMessage>();

            ResponseUserLoginMessage.Error error = ResponseUserLoginMessage.Error.None;
            string userId      = string.Empty;
            string accessToken = string.Empty;
            // Validate by facebook api
            string    url       = "https://graph.facebook.com/" + message.id + "?access_token=" + message.accessToken + "&fields=id,name,email";
            WebClient webClient = new WebClient();
            string    json      = webClient.DownloadString(url);

            json = json.Replace(@"\u0040", "@");
            Dictionary <string, object> dict = Json.Deserialize(json) as Dictionary <string, object>;

            if (dict.ContainsKey("id") && dict.ContainsKey("email"))
            {
                string           email = (string)dict["email"];
                FacebookLoginJob job   = new FacebookLoginJob(Database, message.id, email);
                job.Start();
                yield return(StartCoroutine(job.WaitFor()));

                userId = job.result;
            }
            // Response clients
            if (string.IsNullOrEmpty(userId))
            {
                error  = ResponseUserLoginMessage.Error.InvalidUsernameOrPassword;
                userId = string.Empty;
            }
            else if (userPeersByUserId.ContainsKey(userId) || MapContainsUser(userId))
            {
                error  = ResponseUserLoginMessage.Error.AlreadyLogin;
                userId = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                UpdateAccessTokenJob updateAccessTokenJob = new UpdateAccessTokenJob(Database, userId, accessToken);
                updateAccessTokenJob.Start();
                yield return(StartCoroutine(updateAccessTokenJob.WaitFor()));
            }
            ResponseUserLoginMessage responseMessage = new ResponseUserLoginMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseUserLoginMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.userId       = userId;
            responseMessage.accessToken  = accessToken;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseUserLogin, responseMessage);
        }
예제 #2
0
        private IEnumerator HandleRequestValidateAccessTokenRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestValidateAccessTokenMessage message = messageHandler.ReadMessage <RequestValidateAccessTokenMessage>();

            ResponseValidateAccessTokenMessage.Error error = ResponseValidateAccessTokenMessage.Error.None;
            string userId      = message.userId;
            string accessToken = message.accessToken;
            ValidateAccessTokenJob validateAccessTokenJob = new ValidateAccessTokenJob(Database, userId, accessToken);

            validateAccessTokenJob.Start();
            yield return(StartCoroutine(validateAccessTokenJob.WaitFor()));

            if (!validateAccessTokenJob.result)
            {
                error       = ResponseValidateAccessTokenMessage.Error.InvalidAccessToken;
                userId      = string.Empty;
                accessToken = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo;
                if (userPeersByUserId.TryGetValue(userId, out userPeerInfo))
                {
                    userPeersByUserId.Remove(userPeerInfo.userId);
                    userPeers.Remove(userPeerInfo.connectionId);
                }
                userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                UpdateAccessTokenJob updateAccessTokenJob = new UpdateAccessTokenJob(Database, userId, accessToken);
                updateAccessTokenJob.Start();
                yield return(StartCoroutine(updateAccessTokenJob.WaitFor()));
            }
            ResponseValidateAccessTokenMessage responseMessage = new ResponseValidateAccessTokenMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseValidateAccessTokenMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.userId       = userId;
            responseMessage.accessToken  = accessToken;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseValidateAccessToken, responseMessage);
        }
예제 #3
0
        private IEnumerator HandleRequestUserLoginRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestUserLoginMessage message = messageHandler.ReadMessage <RequestUserLoginMessage>();

            ResponseUserLoginMessage.Error error       = ResponseUserLoginMessage.Error.None;
            ValidateUserLoginJob           validateJob = new ValidateUserLoginJob(Database, message.username, message.password);

            validateJob.Start();
            yield return(StartCoroutine(validateJob.WaitFor()));

            string userId      = validateJob.result;
            string accessToken = string.Empty;

            if (string.IsNullOrEmpty(userId))
            {
                error  = ResponseUserLoginMessage.Error.InvalidUsernameOrPassword;
                userId = string.Empty;
            }
            else if (userPeersByUserId.ContainsKey(userId) || MapContainsUser(userId))
            {
                error  = ResponseUserLoginMessage.Error.AlreadyLogin;
                userId = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                UpdateAccessTokenJob updateAccessTokenJob = new UpdateAccessTokenJob(Database, userId, accessToken);
                updateAccessTokenJob.Start();
                yield return(StartCoroutine(updateAccessTokenJob.WaitFor()));
            }
            ResponseUserLoginMessage responseMessage = new ResponseUserLoginMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseUserLoginMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.userId       = userId;
            responseMessage.accessToken  = accessToken;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseUserLogin, responseMessage);
        }
예제 #4
0
        private IEnumerator HandleRequestUserLogoutRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long                connectionId = messageHandler.connectionId;
            BaseAckMessage      message      = messageHandler.ReadMessage <BaseAckMessage>();
            CentralUserPeerInfo userPeerInfo;

            if (userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                userPeersByUserId.Remove(userPeerInfo.userId);
                userPeers.Remove(connectionId);
                UpdateAccessTokenJob updateAccessTokenJob = new UpdateAccessTokenJob(Database, userPeerInfo.userId, string.Empty);
                updateAccessTokenJob.Start();
                yield return(StartCoroutine(updateAccessTokenJob.WaitFor()));
            }
            BaseAckMessage responseMessage = new BaseAckMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = AckResponseCode.Success;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseUserLogout, responseMessage);
        }