0

对不起我的英语不好。我做了一些测试客户端来测试我们的网络服务器,但我对我的工作不满意。请帮助我改进我的代码。

我想发出串行 HTTP 请求。我有带有测试请求的数组:

Aua.Queries = ko.observableArray([
    new Query("/srv?key=value&key2=someValue", 300, "comment for this request"), 
    new Query("/srv?someKey=SomeValue", 0, "comment for this request")
]);

(在 Aua.Queries 数组中 24 个查询)

我对 Aua.Queries observableArray 的包装:

function Query(query, result, comment) {
    var me = this;
    me.Query = "http://mydomain.com?" + query;
    me.Result = result;
    me.Comment = comment;
    me.Selected = ko.observable(false);
}

当单击发送处理程序时,我这样做:

Aua.SendHandler = function () {
    Aua.Loading(true);
    Aua.Responses([]);
    if (Aua.Advanced() == true) {
        var queries = Aua.Queries();
        console.log("Selected Request Count: ",
            Enumerable.From(queries).Where("$.Selected() == true").Select(function(query) {
                console.log("Result: ", query.Result);
                Aua.Url(query.Query);
                query.Selected(false);
                sendRequest();
            }).Count());
    }
    else if (Aua.RequestCount() > 1) {
        for (var i = Aua.RequestCount(); i > 0; i--) {
            Aua.RequestCount(i);
            setUrlByStatus();
            sendRequest();
        }
    }
    else
        sendRequest();
};

sendRequest处理程序:

function sendRequest() {

    console.log("Sending request to: ", Aua.Url());

    $.ajax(Aua.Url(), {
        dataType: 'text',
        cache: false,
        type: 'GET',
        success: function(data, status, xhr) {
            Aua.Responses.push({
                "Data": data,
                "Status": xhr.status + " " + xhr.statusText
            });
            prettyPrint();
        },
        error: function(xhr, status, data) {
            Aua.Responses.push({
                "Data": data,
                "Status": xhr.status + " " + xhr.statusText
            });
            prettyPrint();
            console.log("Error: ", Aua.Responses());
        },
        complete: function() {
            Aua.Loading(false);
        }
    });
}

设置请求格式的类型。

function setUrlByStatus() {
    if (Aua.RequestStatus() === "error")
        Aua.Url(Aua.Queries()[Math.floor(Math.random() * Aua.Queries().length)].Query);
    else
        Aua.Url(
            "http://mydomain.com?" +
                "action=" + Aua.Action() +
                "&number=" + Aua.Number());
}

A 有 3 种类型的发送请求。

  1. 从 Aua.Queries 数组发送多个请求。
  2. 根据计数发送请求。
  3. 并在输入中发送 1 个请求。
4

1 回答 1

0

我做我自己。

我学会了如何组织我的代码

/*
    Client requests for tes our server

Make different demands on the Serer.
Query setings:
     Send something that is in the input.
     Send to a specified number of times queries and allow the user to choose what kind of answer he wants, success or error.
         For successful actions are random data request: email, account, paper.
     Send requests from the table.

     All the answers to print. Prettify XML response.

     After the data to make a progress report
         The following table show: Status Code, Comments, Quantity.

     HTTP status display for each response received and paint the fatal errors in red.
*/

(function (global, undefined) {

    function ajaxObject(url, query, response, loading) {

        console.log("Query: ", query());

        var catched = query();
        return $.ajax({
            cache: false,
            dataType: "text",
            type: "GET",
            url: url() + catched,
            success: function (data, status, xhr) {
                response.push({
                    Data: data,
                    Query: catched,
                    Status: xhr.status + ' ' + xhr.statusText
                });
            },
            error: function (xhr, status, data) {
                response.push({
                    Data: data,
                    Query: catched,
                    Status: xhr.status + " " + xhr.statusText
                });
            },
            complete: function () {
                loading(false);
                prettyPrint();
            }
        });
    }

    function Query(query, result, comment) {
        var me = this;
        me.Query = query;
        me.Result = result;
        me.Comment = comment;
        me.Selected = ko.observable(false);
        me.Describtion = ko.observable(comment);
        me.DescribtionComment = function (data) { me.Describtion(data.Comment); };
        me.DescribtionQuery = function (data) { me.Describtion(data.Query); };
    }

    var queries = [
        new Query("/zibaba?paper=go&id=1&account=someacc&date=20130221174323", 453, "Error. Expected parameter test"),
        new Query("/zibaba?paper=go&account=someacc", 0, "Expected parameter test"),
        new Query("/zibaba?paper=do&id=3&account=someacc&date=20130221174323", 1, " Forbidden"))
    ];

    var Aua = global.Aua = new function Aua() { };
    Aua.Configuration = {
        Advanced: {
            Queries: queries,
            QueriesSelectAll: ko.observable(false),
            Toggle: ko.observable(false)
        },
        Counter: ko.observable(1),
        Query: ko.observable(),
        Url: ko.observable(),
        Status: ko.observable("success")
    };
    Aua.Configuration.Advanced.QueriesSelectAll.subscribe(function () {
        var qs = Aua.Configuration.Advanced.Queries;
        for (var i = 0; i < qs.length; i++)
            qs[i].Selected(!qs[i].Selected());
    });
    Aua.IsLoading = ko.observable(false);
    Aua.SendHandler = function() {
        Aua.IsLoading(true);
        Aua.Responses([]);
        if (Aua.Configuration.Advanced.Toggle() === true) {
            $.when(Enumerable.From(Aua.Configuration.Advanced.Queries)
                .Where("$.Selected() === true")
                .Select(function(current) {
                    Aua.Configuration.Query(current.Query);
                    return ajaxObject(Aua.Configuration.Url, Aua.Configuration.Query, Aua.Responses, Aua.IsLoading);
                }).ToArray());
        } else {
            $.when(Enumerable.Generate(function() {
                var papers;

                if (Aua.Configuration.Status() == "success") {
                    papers = ["do", "go"];

                    Aua.Configuration.Query(
                        "?account=" + Math.floor(Math.random() * 1234567 + 1) +
                            ("&paper=" + papers[Math.floor(Math.random() * papers.length)]) +
                            ("&id=" + Math.floor(Math.random() * 123754 + 1)));
                } else if (Aua.Configuration.Status() == "error") {
                    papers = ["do", "go", "justdoit"];
                    var accounts = [Math.floor(Math.random() * 1234567 + 1), " ", Math.floor(Math.random() * 4532 + 1)],
                        ids = [" ", (Math.floor(Math.random() * 123754 + 1)), (Math.floor(Math.random()))];

                    Aua.Configuration.Query(
                        "?account=" + accounts[Math.floor(Math.random() * accounts.length)] +
                            "&paper=" + (papers[Math.floor(Math.random() * papers.length)]) +
                            "&id=" + (ids[Math.floor(Math.random() * ids.length)]));
                }

                return ajaxObject(Aua.Configuration.Url, Aua.Configuration.Query, Aua.Responses, Aua.IsLoading);
            }, Aua.Configuration.Counter()).ToArray());
        }
    };
    Aua.ResetHandler = function () { location.reload(); };
    Aua.Statistics = {
        Code: ko.observable(0),
        Comment: ko.observable(0),
        Quantity: ko.observable(0)
    };
    Aua.Responses = ko.observableArray([{
        Data: ko.observable(),
        Status: ko.observable(-1)
    }]);
    Aua.Responses([]);

    $(document).on("ready", function () {
        ko.applyBindings(Aua);
    });
 })(window);

对不起我的英语不好

于 2013-04-24T14:19:48.423 回答