Skip to content

Commit 5c4c681

Browse files
committed
Updated client
1 parent 7ca0606 commit 5c4c681

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1055
-2859
lines changed

support/socket.io-client/README.md

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,25 @@ If you are serving you .swf from a other domain than socket.io.js you will need
6464

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

67-
IMPORTANT! When checking out the git repo, make sure to include the submodules. One way to do it is:
68-
69-
git clone [repo] --recursive
70-
71-
Another, once cloned
72-
73-
git submodule update --init --recursive
74-
7567
### Documentation
7668

7769
#### io.Socket
7870

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

81-
Options:
73+
##### Options:
74+
75+
- *secure*
76+
77+
false
78+
79+
Use secure connections
8280

8381
- *port*
8482

8583
Current port or 80
8684

87-
The port `socket.io` server is attached to (defaults to the document.location port)
85+
The port `socket.io` server is attached to (defaults to the document.location port).
8886

8987
- *resource*
9088

@@ -94,9 +92,9 @@ Options:
9492

9593
- *transports*
9694

97-
['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling']
95+
['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
9896

99-
A list of the transports to attempt to utilize (in order of preference)
97+
A list of the transports to attempt to utilize (in order of preference).
10098

10199
- *transportOptions*
102100

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

112-
Properties:
110+
- *rememberTransport*
111+
112+
true
113+
114+
A boolean indicating if the utilized transport should be remembered in a cookie.
115+
116+
- *connectTimeout*
117+
118+
5000
119+
120+
The amount of miliseconds a transport has to create a connection before we consider it timed out.
121+
122+
- *tryTransportsOnConnectTimeout*
123+
124+
true
125+
126+
A boolean indicating if we should try other transports when the connectTimeout occurs.
127+
128+
- *reconnect*
129+
130+
true
131+
132+
A boolean indicating if we should automatically reconnect if a connection is disconnected.
133+
134+
- *reconnectionDelay*
135+
136+
500
137+
138+
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).
139+
140+
141+
- *maxReconnectionAttempts*
142+
143+
10
144+
145+
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.
146+
147+
##### Properties:
113148

114149
- *options*
115150

116-
The passed in options combined with the defaults
151+
The passed in options combined with the defaults.
117152

118153
- *connected*
119154

@@ -122,46 +157,50 @@ Properties:
122157
- *connecting*
123158

124159
Whether the socket is connecting or not.
160+
161+
- *reconnecting*
162+
163+
Whether we are reconnecting or not.
125164

126165
- *transport*
127166

128167
The transport instance.
129168

130-
Methods:
169+
##### Methods:
131170

132171
- *connect*
133172

134-
Establishes a connection
173+
Establishes a connection.
135174

136175
- *send(message)*
137176

138177
A string of data to send.
139178

140179
- *disconnect*
141180

142-
Closes the connection
181+
Closes the connection.
143182

144183
- *on(event, λ)*
145184

146-
Adds a listener for the event *event*
185+
Adds a listener for the event *event*.
147186

148187
- *removeEvent(event, λ)*
149188

150-
Removes the listener λ for the event *event*
189+
Removes the listener λ for the event *event*.
151190

152-
Events:
191+
##### Events:
153192

154193
- *connect*
155194

156-
Fired when the connection is established and the handshake successful
195+
Fired when the connection is established and the handshake successful.
157196

158197
- *connecting(transport_type)*
159198

160-
Fired when a connection is attempted, passing the transport name
199+
Fired when a connection is attempted, passing the transport name.
161200

162201
- *connect_failed*
163202

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

179218
Fired when the connection is considered disconnected.
219+
220+
- *reconnect(transport_type,reconnectionAttempts)*
221+
222+
Fired when the connection has been re-established. This only fires if the `reconnect` option is set.
223+
224+
- *reconnecting(reconnectionDelay,reconnectionAttempts)*
225+
226+
Fired when a reconnection is attempted, passing the next delay for the next reconnection.
180227

181-
### Credits
228+
229+
- *reconnect_failed*
230+
231+
Fired when all reconnection attempts have failed and we where unsucessfull in reconnecting to the server.
232+
233+
### Contributors
182234

183235
Guillermo Rauch <guillermo@learnboost.com>
184236

237+
Arnout Kazemier <info@3rd-eden.com>
238+
185239
### License
186240

187241
(The MIT License)

support/socket.io-client/bin/build

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
*/
1515

1616
var fs = require('fs'),
17-
sys = require('sys'),
1817
socket = require('../lib/io'),
18+
jsp = require('../lib/vendor/uglifyjs/lib/parse-js'),
19+
pro = require("../lib/vendor/uglifyjs/lib/process"),
20+
ast,
1921
files = [
2022
'io.js',
2123
'util.js',
@@ -29,22 +31,29 @@ var fs = require('fs'),
2931
'transports/jsonp-polling.js',
3032
'socket.js',
3133
'vendor/web-socket-js/swfobject.js',
32-
'vendor/web-socket-js/FABridge.js',
3334
'vendor/web-socket-js/web_socket.js'
3435
],
35-
content = "/** Socket.IO "+ socket.io.version +" - Built with build.js */\n";
36+
content = "/** Socket.IO "+ socket.io.version +" - Built with build.js */\n",
37+
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";
3638

37-
sys.log('Reading files…');
39+
console.log('Reading files…');
3840

3941
files.forEach(function(file){
4042
var path = __dirname + '/../lib/' + file;
41-
sys.log (' + ' + path);
43+
console.log(' + ' + path);
4244
content += fs.readFileSync(path) + "\n";
4345
});
4446

45-
sys.log('Generating…');
47+
console.log('Generating…');
4648

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

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

support/socket.io-client/lib/io.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
77
*/
88

9-
this.io = {
9+
var io = this.io = {
1010
version: '0.6.2',
1111

1212
setPath: function(path){

support/socket.io-client/lib/socket.js

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
(function(){
10+
var io = this.io;
1011

1112
var Socket = io.Socket = function(host, options){
1213
this.host = host || document.domain;
@@ -25,6 +26,9 @@
2526
}
2627
},
2728
connectTimeout: 5000,
29+
reconnect: true,
30+
reconnectionDelay: 500,
31+
maxReconnectionAttempts: 10,
2832
tryTransportsOnConnectTimeout: true,
2933
rememberTransport: true
3034
};
@@ -57,15 +61,15 @@
5761

5862
Socket.prototype.connect = function(){
5963
if (this.transport && !this.connected){
60-
if (this.connecting) this.disconnect();
64+
if (this.connecting) this.disconnect(true);
6165
this.connecting = true;
6266
this.emit('connecting', [this.transport.type]);
6367
this.transport.connect();
6468
if (this.options.connectTimeout){
6569
var self = this;
6670
this.connectTimeoutTimer = setTimeout(function(){
6771
if (!self.connected){
68-
self.disconnect();
72+
self.disconnect(true);
6973
if (self.options.tryTransportsOnConnectTimeout && !self._rememberedTransport){
7074
if(!self._remainingTransports) self._remainingTransports = self.options.transports.slice(0);
7175
var transports = self._remainingTransports;
@@ -90,8 +94,9 @@
9094
return this;
9195
};
9296

93-
Socket.prototype.disconnect = function(){
97+
Socket.prototype.disconnect = function(reconnect){
9498
if (this.connectTimeoutTimer) clearTimeout(this.connectTimeoutTimer);
99+
if (!reconnect) this.options.reconnect = false;
95100
this.transport.disconnect();
96101
return this;
97102
};
@@ -133,7 +138,8 @@
133138
};
134139

135140
Socket.prototype._isXDomain = function(){
136-
return this.host !== document.domain;
141+
var locPort = window.location.port || 80;
142+
return this.host !== document.domain || this.options.port != locPort;
137143
};
138144

139145
Socket.prototype._onConnect = function(){
@@ -153,11 +159,69 @@
153159
this.connected = false;
154160
this.connecting = false;
155161
this._queueStack = [];
156-
if (wasConnected) this.emit('disconnect');
162+
if (wasConnected){
163+
this.emit('disconnect');
164+
if (this.options.reconnect && !this.reconnecting) this._onReconnect();
165+
}
166+
};
167+
168+
Socket.prototype._onReconnect = function(){
169+
this.reconnecting = true;
170+
this.reconnectionAttempts = 0;
171+
this.reconnectionDelay = this.options.reconnectionDelay;
172+
173+
var self = this
174+
, tryTransportsOnConnectTimeout = this.options.tryTransportsOnConnectTimeout
175+
, rememberTransport = this.options.rememberTransport;
176+
177+
function reset(){
178+
if(self.connected) self.emit('reconnect',[self.transport.type,self.reconnectionAttempts]);
179+
self.removeEvent('connect_failed', maybeReconnect).removeEvent('connect', maybeReconnect);
180+
delete self.reconnecting;
181+
delete self.reconnectionAttempts;
182+
delete self.reconnectionDelay;
183+
delete self.reconnectionTimer;
184+
delete self.redoTransports;
185+
self.options.tryTransportsOnConnectTimeout = tryTransportsOnConnectTimeout;
186+
self.options.rememberTransport = rememberTransport;
187+
188+
return;
189+
};
190+
191+
function maybeReconnect(){
192+
if (!self.reconnecting) return;
193+
if (!self.connected){
194+
if (self.connecting && self.reconnecting) return self.reconnectionTimer = setTimeout(maybeReconnect, 1000);
195+
196+
if (self.reconnectionAttempts++ >= self.options.maxReconnectionAttempts){
197+
if (!self.redoTransports){
198+
self.on('connect_failed', maybeReconnect);
199+
self.options.tryTransportsOnConnectTimeout = true;
200+
self.transport = self.getTransport(self.options.transports); // overwrite with all enabled transports
201+
self.redoTransports = true;
202+
self.connect();
203+
} else {
204+
self.emit('reconnect_failed');
205+
reset();
206+
}
207+
} else {
208+
self.reconnectionDelay *= 2; // exponential backoff
209+
self.connect();
210+
self.emit('reconnecting', [self.reconnectionDelay,self.reconnectionAttempts]);
211+
self.reconnectionTimer = setTimeout(maybeReconnect, self.reconnectionDelay);
212+
}
213+
} else {
214+
reset();
215+
}
216+
};
217+
this.options.tryTransportsOnConnectTimeout = false;
218+
this.reconnectionTimer = setTimeout(maybeReconnect, this.reconnectionDelay);
219+
220+
this.on('connect', maybeReconnect);
157221
};
158222

159223
Socket.prototype.fire = Socket.prototype.emit;
160-
161224
Socket.prototype.addListener = Socket.prototype.addEvent = Socket.prototype.addEventListener = Socket.prototype.on;
225+
Socket.prototype.removeListener = Socket.prototype.removeEventListener = Socket.prototype.removeEvent;
162226

163-
})();
227+
})();

support/socket.io-client/lib/transport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// abstract
1010

1111
(function(){
12+
var io = this.io;
1213

1314
var frame = '~m~',
1415

support/socket.io-client/lib/transports/flashsocket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
(function(){
10+
var io = this.io;
1011

1112
var Flashsocket = io.Transport.flashsocket = function(){
1213
io.Transport.websocket.apply(this, arguments);

0 commit comments

Comments
 (0)