NGiNX Http Push Module - 广播表现随机

时间:2011-03-11 13:37:34

标签: php nginx

症状:我将“push_listener_concurrency”设置为“广播”,但只有几个(随机数量)订阅者收到消息。有人在处理这类问题吗?测试用例非常直接我向'/publisher?cid=test'频道'发布'并且从'/listener?cid=test'获取'GET'当每个频道只有一个客户端时,一切似乎都有效...

1 个答案:

答案 0 :(得分:1)

以防万一有人会遇到同样的问题。

标题出现问题。要使用jQuery正确地监听nginx http push模块,你应该使用一个正确处理头文件的监听器,这是我的代码:

function Listener(url, successCallback, failureCallback) {
    var scope = this;
    var etag = 0, lastModified = 0;

    var launched = false;

    var failure = false;

    this.successTimeout = 0;
    this.failureTimeout = 5000;

    var getTimeout = function () {
        return failure ? this.failureTimeout : this.successTimeout;
    };

    var listen = function () {
        $.ajax(scope.ajaxOptions);
    }

    var beforeSend = function (jqXHR) {
        jqXHR.setRequestHeader("If-None-Match", etag);
        jqXHR.setRequestHeader("If-Modified-Since", lastModified);
    };

    var complete = function (jqXHR) {
        var timeout = getTimeout();

        etag = jqXHR.getResponseHeader('Etag');
        lastModified = jqXHR.getResponseHeader('Last-Modified');

        var timeout = jqXHR.statusText == 'success' ? scope.successTimeout : scope.failureTimeout;

        if (timeout > 0) {
            setTimeout(listen, timeout);
        } else {
            listen();
        }
    };

    this.ajaxOptions = {
        url : url,
        type : 'GET',
        async : true,
        error : failureCallback,
        success : successCallback,
        dataType : 'json',
        complete : complete,
        beforeSend : beforeSend,
        timeout: 1000 * 60 * 60 * 24
    };

    this.start = function (timeout) {
        if (!launched) {
            if (typeof(timeout) == 'undefined' || timeout == 0) {
                listen();
            } else {
                setTimeout(listen, timeout);
            }
            launched = true;
        }
    };
}