多个 HTTP 客户端请求不存储会话数据

多个 HTTP 客户端请求不存储会话数据

问题描述:

我有一个问题,我想知道代码是否有可能快速创建单独的会话 ID,让我详细说明.我有两个单独的 HTTP 客户端,它们一个接一个地执行(见下面的代码).我遇到的奇怪问题是在第二个 HTTP 客户端请求中,我所做的只是检索一些会话数据.然而,有时它返回的数据很好,有时会话信息未定义,这会导致无休止的问题.一旦我删除了第二个 Http 客户端,问题就不再发生.

I have an issue and i wonder is it possible that the code is to fast that its creating separate session ids, let me elaborate. I have two separate HTTP Clients that perform one after each other (see code below). The strange issue i have is in the second HTTP client request all i am doing is retrieving some session data. However sometimes it returns the data fine and other times the session info is undefined, which is causing no end of problems. Once i remove the second Http client the issue no longer occurs.

一些研究我认为这可能归结为异步客户端,我可以为下一个操作重新使用相同的 Http 客户端变量并且会话数据将被保留吗?任何建议或知识将不胜感激.

A bit of research i think it could be down to asynchronous client, could i re-use the same Http client variable for the next operation and session data will be kept? Any suggests or knowledge would be much appreciated.

this.login = function(username, password, loaded, failed, incorrect) {
        var xhr = Ti.Network.createHTTPClient({
            onload : function(e) {
                var response = this.responseText;
                switch(response) {
                case "1":
                    loaded();
                    break;
                case "0":
                    incorrect();
                    break;
                case "2":
                    incorrect();
                    break;
                case "3":
                    incorrect();
                    break;
                default:
                    failed();
                }
            },
            onerror : function(e) {
                failed(e);
            },
            timeout : 5000,
            validatesSecureCertificate : false
        });
        xhr.open('POST', this.url, true);
        xhr.send({
            'action' : 'login',
            'email' : username,
            'password' : password,
        });

        var getdb = Ti.Network.createHTTPClient({
            onload : function(e) {
                var response = this.responseText;
                Ti.App.Properties.setString('name', response);
            },
            onerror : function(e) {
                failed(e);
            },
            timeout : 5000,
            validatesSecureCertificate : false
        });
        getdb.open('POST', this.url, true);
        getdb.send({
            'action' : 'get_name',
            'device' : 'mobile'     
        });

    };

您的问题是您同时执行两个调用.所以执行顺序是未知的.您需要做的是在第一个完成后调用第二个.为此,您需要在第一个的回调中添加第二个 http 调用.

Your problem is the fact that you're executing both calls at the same time. So the order of execution is unknown. What you need to do is call the 2nd after the first has finished. For this to work you will need to add the second http call within the callback of the first.

为了让你的代码更有条理,我建议使用函数!使其也更具可读性.

And to make your code more organised I recommend using functions! Makes it also more readable.

function doBothCalls(){
    doFirstCallFunction(function(){
        doSecondCallFunction();
    }
}

doFirstCallFunction 然后得到一个回调函数,这个回调函数应该在第一个进入 http 回调后调用.

The doFirstCallFunction then gets a callback function, this callback function you should call after the first one has gotten into the http callback.