javascript - Firefox randomly close XMLHttpRequest connection if inactive. Why? -
in javascript class, xmlhttprequest connect server. server sending data, slowly. work fine in chromium, firefox close connection after random time (between ~4s , ~70s).
why firefox close connection? , how avoid that?
simplified js code:
var options = {}; options['header']= { 'cache-control':'no-cache, max-age=0', 'content-type': 'application/octet-stream', 'content-disposition': 'inline' }; // request information this.http = new xmlhttprequest(); this.http.onreadystatechange = _streamingresponse.bind(this); this.http.open('post', url, true); (var in options['header']) { this.http.setrequestheader(i, options['header'][i]); } this.http.send('');
for php part, like:
sleep(200); //wait long time, firefox close socket.
if server send every few seconds (<5s) connection stay alive "forever". if no data sent, firefox close connection.
the connection close with: - readystate = 4 - status = 0
the server seem correct, in chromium work correctly.
full test code:
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; // request information this.http = new xmlhttprequest(); this.http.onreadystatechange = _streamingresponse.bind(this); this.http.open('post', url, true); (var in options['header']) { this.http.setrequestheader(i, options['header'][i]); } this.http.send(''); log('request sent!'); } req = new request('./test.php'); </script> </html>
test.php
<?php $timer = 60; ignore_user_abort(true); set_time_limit(0); // turn off output buffering , 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 streaming header('content-type: application/octet-stream'); flush(); // send information sleep($timer); echo '<yes></yes>'; flush(); ?>
additional note: firefox 43.0.03, chromium 47.0.2526
edited:
setting callback timeout not trigger. conclude not timeout.
this.http.timeout = 2000; this.http.ontimeout = _streamingtimeout.bind(this);
after searching further, found bug in mozilla seem responsible of behaviour. should solved in version 45, until then, have lead it.
even if bug seem related ipv6, have same issue using 127.0.0.1. however, firefox developer edition (v 45) problem seem solved.
why firefox close connection?
it should not. ref: https://bugzilla.mozilla.org/show_bug.cgi?id=1240319
how solve it?
except send data every 3-4 seconds maintain connection open, have no idea.
Comments
Post a Comment