Skip to content

Commit

Permalink
Updated client
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Mar 30, 2011
1 parent 7ca0606 commit 5c4c681
Show file tree
Hide file tree
Showing 81 changed files with 1,055 additions and 2,859 deletions.
102 changes: 78 additions & 24 deletions support/socket.io-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,25 @@ If you are serving you .swf from a other domain than socket.io.js you will need

The insecure version can be found [here](http://github.com/gimite/web-socket-js/blob/master/WebSocketMainInsecure.zip).

IMPORTANT! When checking out the git repo, make sure to include the submodules. One way to do it is:

git clone [repo] --recursive

Another, once cloned

git submodule update --init --recursive

### Documentation

#### io.Socket

new io.Socket(host, [options]);

Options:
##### Options:

- *secure*

false

Use secure connections

- *port*

Current port or 80

The port `socket.io` server is attached to (defaults to the document.location port)
The port `socket.io` server is attached to (defaults to the document.location port).

- *resource*

Expand All @@ -94,9 +92,9 @@ Options:

- *transports*

['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling']
['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']

A list of the transports to attempt to utilize (in order of preference)
A list of the transports to attempt to utilize (in order of preference).

- *transportOptions*

Expand All @@ -109,11 +107,48 @@ Options:
An object containing (optional) options to pass to each transport.

Properties:
- *rememberTransport*

true

A boolean indicating if the utilized transport should be remembered in a cookie.

- *connectTimeout*

5000

The amount of miliseconds a transport has to create a connection before we consider it timed out.

- *tryTransportsOnConnectTimeout*

true

A boolean indicating if we should try other transports when the connectTimeout occurs.

- *reconnect*

true

A boolean indicating if we should automatically reconnect if a connection is disconnected.

- *reconnectionDelay*

500

The amount of milliseconds before we try to connect to the server again. We are using a exponential back off algorithm for the following reconnections, on each reconnect attempt this value will get multiplied (500 > 1000 > 2000 > 4000 > 8000).


- *maxReconnectionAttempts*

10

The amount of attempts should we make using the current transport to connect to the server? After this we will do one final attempt, and re-try with all enabled transport methods before we give up.

##### Properties:

- *options*

The passed in options combined with the defaults
The passed in options combined with the defaults.

- *connected*

Expand All @@ -122,46 +157,50 @@ Properties:
- *connecting*

Whether the socket is connecting or not.

- *reconnecting*

Whether we are reconnecting or not.

- *transport*

The transport instance.

Methods:
##### Methods:

- *connect*

Establishes a connection
Establishes a connection.

- *send(message)*

A string of data to send.

- *disconnect*

Closes the connection
Closes the connection.

- *on(event, λ)*

Adds a listener for the event *event*
Adds a listener for the event *event*.

- *removeEvent(event, λ)*

Removes the listener λ for the event *event*
Removes the listener λ for the event *event*.

Events:
##### Events:

- *connect*

Fired when the connection is established and the handshake successful
Fired when the connection is established and the handshake successful.

- *connecting(transport_type)*

Fired when a connection is attempted, passing the transport name
Fired when a connection is attempted, passing the transport name.

- *connect_failed*

Fired when the connection timeout occurs after the last connection attempt.
Fired when the connection timeout occurs after the last connection attempt.
This only fires if the `connectTimeout` option is set.
If the `tryTransportsOnConnectTimeout` option is set, this only fires once all
possible transports have been tried.
Expand All @@ -177,11 +216,26 @@ Events:
- *disconnect*

Fired when the connection is considered disconnected.

- *reconnect(transport_type,reconnectionAttempts)*

Fired when the connection has been re-established. This only fires if the `reconnect` option is set.

- *reconnecting(reconnectionDelay,reconnectionAttempts)*

Fired when a reconnection is attempted, passing the next delay for the next reconnection.

### Credits

- *reconnect_failed*

Fired when all reconnection attempts have failed and we where unsucessfull in reconnecting to the server.

### Contributors

Guillermo Rauch <guillermo@learnboost.com>

Arnout Kazemier <info@3rd-eden.com>

### License

(The MIT License)
Expand Down
25 changes: 17 additions & 8 deletions support/socket.io-client/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/

var fs = require('fs'),
sys = require('sys'),
socket = require('../lib/io'),
jsp = require('../lib/vendor/uglifyjs/lib/parse-js'),
pro = require("../lib/vendor/uglifyjs/lib/process"),
ast,
files = [
'io.js',
'util.js',
Expand All @@ -29,22 +31,29 @@ var fs = require('fs'),
'transports/jsonp-polling.js',
'socket.js',
'vendor/web-socket-js/swfobject.js',
'vendor/web-socket-js/FABridge.js',
'vendor/web-socket-js/web_socket.js'
],
content = "/** Socket.IO "+ socket.io.version +" - Built with build.js */\n";
content = "/** Socket.IO "+ socket.io.version +" - Built with build.js */\n",
license = "/* Socket.IO.min "+ socket.io.version +" @author Guillermo Rauch <guillermo@learnboost.com>, @license The MIT license., @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com> */\n";

sys.log('Reading files…');
console.log('Reading files…');

files.forEach(function(file){
var path = __dirname + '/../lib/' + file;
sys.log (' + ' + path);
console.log(' + ' + path);
content += fs.readFileSync(path) + "\n";
});

sys.log('Generating…');
console.log('Generating…');

fs.write(fs.openSync(__dirname + '/../socket.io.js', 'w'), content, 0, 'utf8');
sys.log(' + ' + __dirname + '/../socket.io.js');
console.log(' + ' + __dirname + '/../socket.io.js');

sys.log('All done!');
console.log('Uglyfying…');
ast = jsp.parse(content);
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast);
fs.write(fs.openSync(__dirname + '/../socket.io.min.js', 'w'), license + pro.gen_code(ast), 0, 'utf8');
console.log(' + ' + __dirname + '/../socket.io.min.js');

console.log('All done!');
2 changes: 1 addition & 1 deletion support/socket.io-client/lib/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
*/

this.io = {
var io = this.io = {
version: '0.6.2',

setPath: function(path){
Expand Down
78 changes: 71 additions & 7 deletions support/socket.io-client/lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

(function(){
var io = this.io;

var Socket = io.Socket = function(host, options){
this.host = host || document.domain;
Expand All @@ -25,6 +26,9 @@
}
},
connectTimeout: 5000,
reconnect: true,
reconnectionDelay: 500,
maxReconnectionAttempts: 10,
tryTransportsOnConnectTimeout: true,
rememberTransport: true
};
Expand Down Expand Up @@ -57,15 +61,15 @@

Socket.prototype.connect = function(){
if (this.transport && !this.connected){
if (this.connecting) this.disconnect();
if (this.connecting) this.disconnect(true);
this.connecting = true;
this.emit('connecting', [this.transport.type]);
this.transport.connect();
if (this.options.connectTimeout){
var self = this;
this.connectTimeoutTimer = setTimeout(function(){
if (!self.connected){
self.disconnect();
self.disconnect(true);
if (self.options.tryTransportsOnConnectTimeout && !self._rememberedTransport){
if(!self._remainingTransports) self._remainingTransports = self.options.transports.slice(0);
var transports = self._remainingTransports;
Expand All @@ -90,8 +94,9 @@
return this;
};

Socket.prototype.disconnect = function(){
Socket.prototype.disconnect = function(reconnect){
if (this.connectTimeoutTimer) clearTimeout(this.connectTimeoutTimer);
if (!reconnect) this.options.reconnect = false;
this.transport.disconnect();
return this;
};
Expand Down Expand Up @@ -133,7 +138,8 @@
};

Socket.prototype._isXDomain = function(){
return this.host !== document.domain;
var locPort = window.location.port || 80;
return this.host !== document.domain || this.options.port != locPort;
};

Socket.prototype._onConnect = function(){
Expand All @@ -153,11 +159,69 @@
this.connected = false;
this.connecting = false;
this._queueStack = [];
if (wasConnected) this.emit('disconnect');
if (wasConnected){
this.emit('disconnect');
if (this.options.reconnect && !this.reconnecting) this._onReconnect();
}
};

Socket.prototype._onReconnect = function(){
this.reconnecting = true;
this.reconnectionAttempts = 0;
this.reconnectionDelay = this.options.reconnectionDelay;

var self = this
, tryTransportsOnConnectTimeout = this.options.tryTransportsOnConnectTimeout
, rememberTransport = this.options.rememberTransport;

function reset(){
if(self.connected) self.emit('reconnect',[self.transport.type,self.reconnectionAttempts]);
self.removeEvent('connect_failed', maybeReconnect).removeEvent('connect', maybeReconnect);
delete self.reconnecting;
delete self.reconnectionAttempts;
delete self.reconnectionDelay;
delete self.reconnectionTimer;
delete self.redoTransports;
self.options.tryTransportsOnConnectTimeout = tryTransportsOnConnectTimeout;
self.options.rememberTransport = rememberTransport;

return;
};

function maybeReconnect(){
if (!self.reconnecting) return;
if (!self.connected){
if (self.connecting && self.reconnecting) return self.reconnectionTimer = setTimeout(maybeReconnect, 1000);

if (self.reconnectionAttempts++ >= self.options.maxReconnectionAttempts){
if (!self.redoTransports){
self.on('connect_failed', maybeReconnect);
self.options.tryTransportsOnConnectTimeout = true;
self.transport = self.getTransport(self.options.transports); // overwrite with all enabled transports
self.redoTransports = true;
self.connect();
} else {
self.emit('reconnect_failed');
reset();
}
} else {
self.reconnectionDelay *= 2; // exponential backoff
self.connect();
self.emit('reconnecting', [self.reconnectionDelay,self.reconnectionAttempts]);
self.reconnectionTimer = setTimeout(maybeReconnect, self.reconnectionDelay);
}
} else {
reset();
}
};
this.options.tryTransportsOnConnectTimeout = false;
this.reconnectionTimer = setTimeout(maybeReconnect, this.reconnectionDelay);

this.on('connect', maybeReconnect);
};

Socket.prototype.fire = Socket.prototype.emit;

Socket.prototype.addListener = Socket.prototype.addEvent = Socket.prototype.addEventListener = Socket.prototype.on;
Socket.prototype.removeListener = Socket.prototype.removeEventListener = Socket.prototype.removeEvent;

})();
})();
1 change: 1 addition & 0 deletions support/socket.io-client/lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// abstract

(function(){
var io = this.io;

var frame = '~m~',

Expand Down
1 change: 1 addition & 0 deletions support/socket.io-client/lib/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

(function(){
var io = this.io;

var Flashsocket = io.Transport.flashsocket = function(){
io.Transport.websocket.apply(this, arguments);
Expand Down
Loading

0 comments on commit 5c4c681

Please sign in to comment.