如果不活动,Firefox会随机关闭XMLHttpRequest连接.为什么?

Adr*_*ire 7 javascript firefox http xmlhttprequest

在JavaScript类中,XMLHttpRequest连接到服务器.服务器正在缓慢地发送数据.这在Chromium中运行良好,但Firefox在随机时间后(约4s到~70s之间)关闭连接.

为什么Firefox会关闭连接?以及如何避免这种情况?

简化的JS代码:

    var options = {};
    options['header']=
        { 'Cache-Control':'no-cache, max-age=0', 
            'Content-type': 'application/octet-stream',
            'Content-Disposition': 'inline'
        };

    // Get request information
    this.http = new XMLHttpRequest();
    this.http.onreadystatechange = _streamingResponse.bind(this);
    this.http.open('post', url, true);
    for (var i in options['header'])
    {
        this.http.setRequestHeader(i, options['header'][i]);
    }
    this.http.send('');
Run Code Online (Sandbox Code Playgroud)

对于PHP部分,类似于:

sleep(200); //wait long time, so firefox close the socket.
Run Code Online (Sandbox Code Playgroud)

如果服务器每隔几秒钟(<5s)发送一次内容,则连接会"永远"保持活动状态.但是如果没有发送数据,Firefox会关闭连接.

连接关闭: - readyState = 4 - status = 0

服务器似乎是正确的,因为在Chromium中它可以正常工作.

完整的测试代码:

的test.html

<html>
<header>
</header>
<body>
</body>

<script type="application/javascript">

    function log( msg )
    {
        document.body.appendChild(document.createElement('div').appendChild(document.createTextNode(msg)));
        document.body.appendChild(document.createElement('br'));
    }

    function request(url)
    {
        function _streamingResponse()
        {
            if (4==this.http.readyState)
            {
                log('Done: ' + this.http.status);
            }
            else if (3==this.http.readyState)
            {
                var text = this.http.response.substr(this.lastRequestPos);
                this.lastRequestPos = this.http.response.length;
                log('Update: ' + text);
            }
        }

        var options = {};
        options['header']=
            { 'Cache-Control':'no-cache, max-age=0', 
                'Content-type': 'application/octet-stream',
                'Content-Disposition': 'inline'
            };

        this.lastRequestPos=0;

        // Get request information
        this.http = new XMLHttpRequest();
        this.http.onreadystatechange = _streamingResponse.bind(this);
        this.http.open('post', url, true);
        for (var i in options['header'])
        {
            this.http.setRequestHeader(i, options['header'][i]);
        }
        this.http.send('');
        log('Request sent!');
    }

    req = new request('./test.php');
</script>
</html>
Run Code Online (Sandbox Code Playgroud)

test.php的

<?php

$timer = 60;

ignore_user_abort(true);
set_time_limit(0);

// Turn off output buffering and compression
ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
ini_set('implicit_flush', true);
ob_implicit_flush(true);
while (ob_get_level() > 0) {
    $level = ob_get_level();
    ob_end_clean();
    if (ob_get_level() == $level) break;
}
if (function_exists('apache_setenv')) {
    apache_setenv('no-gzip', '1');
    apache_setenv('dont-vary', '1');
}

// Set header for streaming
header('Content-type: application/octet-stream');
flush();

// Send information
sleep($timer);
echo '<yes></yes>';
flush();

?>
Run Code Online (Sandbox Code Playgroud)

附加说明:Firefox 43.0.03,Chromium 47.0.2526

编辑:

设置超时回调不会触发.我总结说这不是暂停.

this.http.timeout = 2000;
this.http.ontimeout = _streamingTimeout.bind(this);
Run Code Online (Sandbox Code Playgroud)

Adr*_*ire 3

经过进一步搜索,我在 Mozilla 中发现了一个 Bug,它似乎是造成这种行为的原因。这个问题应该在 45 版本中得到解决,但在那之前,我们必须以它为主导。

即使该 bug 看起来只与 Ipv6 有关,但我使用 127.0.0.1 时也遇到同样的问题。然而,随着 Firefox Developer Edition (V 45) 的出现,问题似乎得到了解决。

为什么 Firefox 会关闭连接?

不应该。参考:https ://bugzilla.mozilla.org/show_bug.cgi?id=1240319

怎么解决呢?

除了每 3-4 秒发送数据以保持连接打开之外,我不知道。