Skip to content

Commit

Permalink
Connect timeout (fixes #34)
Browse files Browse the repository at this point in the history
Try different transports upon connect timeout (fixes #35)
  • Loading branch information
rauchg committed Sep 20, 2010
1 parent c73d292 commit c2917c7
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/socket.js
Expand Up @@ -24,6 +24,8 @@
timeout: 25000
}
},
connectTimeout: 5000,
tryTransportsOnConnectTimeout: true,
rememberTransport: true
};
for (var i in options)
Expand All @@ -36,9 +38,9 @@
if (!this.transport && 'console' in window) console.error('No transport available');
};

Socket.prototype.getTransport = function(){
var transports = this.options.transports, match;
if (this.options.rememberTransport){
Socket.prototype.getTransport = function(override){
var transports = override || this.options.transports, match;
if (this.options.rememberTransport && !override){
match = this.options.document.cookie.match('(?:^|;)\\s*socketio=([^;]*)');
if (match) transports = [decodeURIComponent(match[1])];
}
Expand All @@ -57,6 +59,25 @@
if (this.connecting) this.disconnect();
this.connecting = true;
this.transport.connect();
if (this.options.connectTimeout){
var self = this;
setTimeout(function(){
if (!self.connected){
self.disconnect();
if (self.options.tryTransportsOnConnectTimeout){
var remainingTransports = [], transports = this.options.transports;
for (var i = 0, transport; transport = transports[i]; i++){
if (transport == self.transport.type) return;
remainingTransports.push(transport);
}
if (remainingTransports.length){
self.transport = self.getTransport(remainingTransports);
self.connect();
}
}
}
}, this.options.connectTimeout)
}
}
return this;
};
Expand Down

0 comments on commit c2917c7

Please sign in to comment.