Skip to content

Commit

Permalink
Changes in iframe.js to support xhr exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Nov 17, 2010
1 parent 15a9fec commit 4f676c8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
49 changes: 32 additions & 17 deletions iframe.js
Expand Up @@ -93,7 +93,11 @@ function CometClient(host){
xhr.send(null);
}

this.connect = function(channel, onMsg) {
this.connect = function(channel, onMsg, seq) {

if(typeof(seq) != "undefined") {
this.seq = seq;
}

if(this.hasWebSocket) {
return this.wsConnect(channel, onMsg);
Expand All @@ -117,9 +121,11 @@ function CometClient(host){
};
this.socket.onclose = function () {
var comet = this;
window.setTimeout(function() {
comet.wsConnect(channel, onMsg);
}, 1000);
try {
window.setTimeout(function() {
comet.wsConnect(channel, onMsg);
}, 1000);
} catch(e) {}
};
}

Expand All @@ -137,7 +143,10 @@ function CometClient(host){
comet.reconnectionTimeout = window.setTimeout(function() {comet.disconnect(); comet.connect(channel, onMsg);}, 25000);
}

var url = "http://"+this.host+"/subscribe?name="+channel+"&keep="+this.canStream+"&seq="+this.seq;
var url = "http://"+this.host+"/subscribe?name="+channel+"&keep="+this.canStream;
if(this.seq >= 0) {
url += "&seq="+this.seq;
}
comet.xhr.open("get", url, true);
comet.xhr.onreadystatechange = function() {

Expand All @@ -146,18 +155,20 @@ function CometClient(host){
}

if(comet.xhr.readyState == 4 && comet.xhr.status != 200) { // error...
window.setTimeout(function() {comet.connect(channel, onMsg);}, 1000); // reconnect soon.
try {
window.setTimeout(function() {comet.connect(channel, onMsg);}, 1000); // reconnect soon.
} catch(e) {}
return;
}

var data = comet.xhr.responseText;
if(comet.xhr.readyState === 3 || comet.xhr.readyState == 4) {
// console.log("data.length=", data.length);
if(data.length == 0) {
// console.log("readyState=", comet.xhr.readyState, ", length=0");
setTimeout(function() {
comet.connect(channel, onMsg);
}, 1000); // reconnect soon.
try {
window.setTimeout(function() {
comet.connect(channel, onMsg);
}, 1000); // reconnect soon.
} catch(e) {}
return;
}

Expand Down Expand Up @@ -193,9 +204,11 @@ function CometClient(host){
}
} catch(e) {
comet.xhr.abort(); // avoid duplicates upon reconnection
setTimeout(function() {
comet.connect(channel, onMsg);
}, 1000); // reconnect soon.
try{
window.setTimeout(function() {
comet.connect(channel, onMsg);
}, 1000); // reconnect soon.
} catch(e) {}
}


Expand All @@ -209,9 +222,11 @@ function CometClient(host){
if(comet.xhr.readyState == 4) { // reconnect
// if no streaming capability, reconnect directly.
if(this.canStream) {
window.setTimeout(function() {
comet.connect(channel, onMsg);
}, 1000);
try {
window.setTimeout(function() {
comet.connect(channel, onMsg);
}, 1000);
} catch(e) {}
} else {
comet.connect(channel, onMsg);
}
Expand Down
26 changes: 13 additions & 13 deletions src/files.c
Expand Up @@ -73,19 +73,19 @@ file_send_libjs(struct connection *cx) {

char buffer_start[] = "var comet_domain = '";
char buffer_domain[] = "'; var common_domain = '";
char buffer_js[] = "';\
Comet = {\
init: function(cbLoaded) {\
var iframe = document.createElement('iframe');\
iframe.src = 'http://' + comet_domain + '/iframe?domain=' + common_domain;\
iframe.setAttribute('style', 'display: none');\
document.body.appendChild(iframe);\
\
iframe.onload = function() {\
Comet.Client = function() { return new iframe.contentWindow.CometClient(comet_domain);};\
cbLoaded();\
};\
}\
char buffer_js[] = "';\n\
Comet = {\n\
init: function(cbLoaded) {\n\
var iframe = document.createElement('iframe');\n\
iframe.src = 'http://' + comet_domain + '/iframe?domain=' + common_domain;\n\
iframe.setAttribute('style', 'display: none');\n\
\n\
iframe.onload = function() {\n\
Comet.Client = function() { return new iframe.contentWindow.CometClient(comet_domain);};\n\
cbLoaded();\n\
};\n\
document.body.appendChild(iframe);\n\
}\n\
};";

http_streaming_start_ct(cx, 200, "OK", "text/javascript");
Expand Down
2 changes: 1 addition & 1 deletion src/http-parser
Submodule http-parser updated from 443a6e to 51de89

0 comments on commit 4f676c8

Please sign in to comment.