Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into patch-2
Browse files Browse the repository at this point in the history
Conflicts:
	test/connection.js
  • Loading branch information
nkzawa committed Mar 1, 2014
2 parents 78f1fd1 + 4cdfe18 commit fc4cdb4
Show file tree
Hide file tree
Showing 16 changed files with 3,883 additions and 3,585 deletions.
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
language: node_js
node_js:
- "0.8"
- "0.10"

- '0.10'
- '0.11'
matrix:
fast_finish: true
allow_failures:
- node_js: '0.11'
notifications:
irc: "irc.freenode.org#socket.io"

irc: irc.freenode.org#socket.io
env:
global:
- secure: wn0UtEtmR0+5ecvr8mXaLJey5XZ4a3j2f5EDoBW0ItALKixqRR6wCrdplwp9MMEDGuer11byIy2YJ+HRazgpcdf+3IuHmSvLb5IItbAaEwcjskawarGcYQO8Gy6GffkbYLFsjEhlkFk9+yXPJuws9msY2+9Y0IymgY8rT9O4D98=
- secure: f5EAsqnJNW//oUgShxSGcDQsz6NUbwfkZCKoPymTamjyK69N5/Q70MQ5UrF3gih3ZiwWDK7Cd6T394vU+Jdrf+LdOdzgef3sz8YJbF3HiOOLp5tYuFVOtOMCBkefdJ/JkqFvrBQUgo3klSD6jUz2SdRlwi33iYlYO8mleO8IxRo=
- secure: 0ah52GLAyJYZwJpnYbTscXgSuSoRLQBj/X0mZeeNGofPbQdWyhQoyBJMq77cTZJQ0mPKd3acojRH5DzYO5q0sMMPkN5mW3couqlyMk94PoYgaiTYRj7dzPnegi1myeVTioX9jomwkh1j+kg1diIHz+VHOgp9n7u595F0HnVXRwA=
- secure: MFEtLhbKZKQDsU9h6dXLRSSZfBUWRJgVV4pucS0bSVA4SZ3Cqq405mg83EiUCww0L0jMXkKKojxcbNl5aHHIzWtTDJTiDw1ifQrIWsbSZ4IPFixd98hU9N7fI4BkEiNhN2j5W4iF+52GDr6Msuy9+6bbuUXEWcJnhjrJ4ePNlBs=
9 changes: 7 additions & 2 deletions .zuul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ ui: mocha-bdd
server: ./test/support/server.js
browsers:
- name: chrome
version: latest
version: 29..latest
- name: firefox
version: latest
- name: safari
version: latest
- name: ie
version: 6..latest
version: 10
platform: Windows 2012
- name: ie
version: [6..9, latest]
- name: iphone
version: oldest..latest
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

REPORTER = dot

build:
@./node_modules/.bin/browserify --standalone io -o socket.io.js .
build: socket.io.js

socket.io.js: lib/*.js package.json
@./support/browserify.sh > socket.io.js

test:
@./node_modules/.bin/mocha \
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# socket.io-client

[![Build Status](https://secure.travis-ci.org/LearnBoost/socket.io-client.png)](http://travis-ci.org/LearnBoost/socket.io-client)
[![NPM version](https://badge.fury.io/js/socket.io-client.png)](http://badge.fury.io/js/socket.io-client)

## How to use

Expand All @@ -21,9 +22,7 @@ serve the file `socket.io.js` found at the root of this repository.
</script>
```

Socket.IO is also compatible with
[component](http://github.com/component/component) and
[browserify](http://github.com/substack/browserify).
Socket.IO is compatible with [browserify](http://github.com/substack/browserify).

### Node.JS (server-side usage)

Expand Down Expand Up @@ -94,7 +93,7 @@ Socket.IO is also compatible with

#### Events

- `connect`. Fired upon a succesful connection.
- `connect`. Fired upon a successful connection.
- `connect_error`. Fired upon a connection error.
Parameters:
- `Object` error object
Expand Down
22 changes: 8 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var url = require('./url');
var parser = require('socket.io-parser');
var Manager = require('./manager');
var debug = require('debug')('socket.io-client');

/**
* Module exports.
Expand Down Expand Up @@ -37,28 +38,21 @@ function lookup(uri, opts){

var parsed = url(uri);
var href = parsed.href;
var id = parsed.id;
var io;

if (opts.forceNew || false === opts.multiplex) {
debug('ignoring socket cache for %s', href);
io = Manager(href, opts);
} else {
var id = parsed.id;
if (!cache[id]) cache[id] = Manager(href, opts);
if (!cache[id]) {
debug('new io instance for %s', href);
cache[id] = Manager(href, opts);
}
io = cache[id];
}

return io.socket(parsed.pathname || '/');
}

/**
* Expose standalone client source.
*
* @api public
*/

if ('undefined' == typeof window) {
var read = require('fs').readFileSync;
exports.source = read(__dirname + '/../socket.io.js');
return io.socket(parsed.path);
}

/**
Expand Down
53 changes: 50 additions & 3 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function Manager(socket, opts){
this.engine = socket;
this.connected = 0;
this.attempts = 0;
this.encoding = false;
this.packetBuffer = [];
this.encoder = new parser.Encoder();
this.decoder = new parser.Decoder();
this.open();
}

Expand Down Expand Up @@ -200,6 +204,7 @@ Manager.prototype.onopen = function(){
// add new subs
var socket = this.engine;
this.subs.push(on(socket, 'data', bind(this, 'ondata')));
this.subs.push(on(this.decoder, 'decoded', bind(this, 'ondecoded')));
this.subs.push(on(socket, 'error', bind(this, 'onerror')));
this.subs.push(on(socket, 'close', bind(this, 'onclose')));
};
Expand All @@ -211,9 +216,19 @@ Manager.prototype.onopen = function(){
*/

Manager.prototype.ondata = function(data){
this.emit('packet', parser.decode(data));
this.decoder.add(data);
};

/**
* Called when parser fully decodes a packet.
*
* @api private
*/

Manager.prototype.ondecoded = function(packet) {
this.emit('packet', packet);
}

/**
* Called upon socket error.
*
Expand Down Expand Up @@ -266,18 +281,50 @@ Manager.prototype.destroy = function(socket){

Manager.prototype.packet = function(packet){
debug('writing packet %j', packet);
this.engine.write(parser.encode(packet));
var self = this;

if (!self.encoding) { // encode, then write to engine with result
self.encoding = true;
this.encoder.encode(packet, function(encodedPackets) {
for (var i = 0; i < encodedPackets.length; i++) {
self.engine.write(encodedPackets[i]);
}
self.encoding = false;
self.processPacketQueue();
});
} else { // add packet to the queue
self.packetBuffer.push(packet);
}
};

/**
* Clean up transport subscriptions.
* If packet buffer is non-empty, begins encoding the
* next packet in line.
*
* @api private
*/

Manager.prototype.processPacketQueue = function() {
if (this.packetBuffer.length > 0 && !this.encoding) {
var pack = this.packetBuffer.shift();
this.packet(pack);
}
}

/**
* Clean up transport subscriptions and packet buffer.
*
* @api private
*/

Manager.prototype.cleanup = function(){
var sub;
while (sub = this.subs.shift()) sub.destroy();

this.packetBuffer = [];
this.encoding = false;

this.decoder.destroy();
};

/**
Expand Down
47 changes: 29 additions & 18 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var toArray = require('to-array');
var on = require('./on');
var bind = require('bind');
var debug = require('debug')('socket.io-client:socket');
var hasBin = require('has-binary-data');
var indexOf = require('indexof');

/**
* Module exports.
Expand All @@ -19,13 +21,15 @@ module.exports = exports = Socket;
/**
* Internal events (blacklisted).
* These events can't be emitted by the user.
*
* @api private
*/

var events = exports.events = [
'connect',
'disconnect',
'error'
];
var events = {
connect: 1,
disconnect: 1,
error: 1
};

/**
* Shortcut to `Emitter#emit`.
Expand Down Expand Up @@ -98,22 +102,25 @@ Socket.prototype.send = function(){
*/

Socket.prototype.emit = function(ev){
if (~events.indexOf(ev)) {
if (events.hasOwnProperty(ev)) {
emit.apply(this, arguments);
} else {
var args = toArray(arguments);
var packet = { type: parser.EVENT, data: args };

// event ack callback
if ('function' == typeof args[args.length - 1]) {
debug('emitting packet with ack id %d', this.ids);
this.acks[this.ids] = args.pop();
packet.id = this.ids++;
}
return this;
}

this.packet(packet);
var args = toArray(arguments);
var parserType = parser.EVENT; // default
if (hasBin(args)) { parserType = parser.BINARY_EVENT; } // binary
var packet = { type: parserType, data: args };

// event ack callback
if ('function' == typeof args[args.length - 1]) {
debug('emitting packet with ack id %d', this.ids);
this.acks[this.ids] = args.pop();
packet.id = this.ids++;
}

this.packet(packet);

return this;
};

Expand Down Expand Up @@ -195,6 +202,10 @@ Socket.prototype.onpacket = function(packet){
this.onevent(packet);
break;

case parser.BINARY_EVENT:
this.onevent(packet);
break;

case parser.ACK:
this.onack(packet);
break;
Expand All @@ -220,7 +231,7 @@ Socket.prototype.onevent = function(packet){
var args = packet.data || [];
debug('emitting event %j', args);

if (packet.id) {
if (null != packet.id) {
debug('attaching ack callback to event');
args.push(this.ack(packet.id));
}
Expand Down
37 changes: 22 additions & 15 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,66 @@
* Module dependencies.
*/

var url = require('url');
var parseuri = require('parseuri');
var debug = require('debug')('socket.io-client:url');

/**
* Module exports.
*/

module.exports = parse;
module.exports = url;

/**
* URL parser.
*
* @param {String} url
* @param {Object} An object meant to mimic window.location.
* Defaults to window.location.
* @api public
*/

function parse(uri){
function url(uri, loc){
var obj = uri;

// default to window's location
if (null == uri) uri = location.protocol + '//' + location.host;
// default to window.location
var loc = loc || global.location;
if (null == uri) uri = loc.protocol + '//' + loc.hostname;

// parse string
// relative path support
if ('string' == typeof uri) {
if ('/' == uri.charAt(0)) {
if ('undefined' != typeof location) {
uri = location.hostname + uri;
if ('undefined' != typeof loc) {
uri = loc.hostname + uri;
}
}

// allow for `localhost:3000`
if (!/^(https?|wss?):\/\//.test(uri)) {
debug('protocol-less url %s', uri);
if ('undefined' != typeof location) {
uri = location.protocol + '//' + uri;
if ('undefined' != typeof loc) {
uri = loc.protocol + '//' + uri;
} else {
uri = 'https://' + uri;
}
}

// parse
debug('parse %s', uri);
obj = url.parse(uri);
obj = parseuri(uri);
}

// make sure we treat `localhost:80` and `localhost` equally
if ((/(http|ws):/.test(obj.protocol) && 80 == obj.port) ||
(/(http|ws)s:/.test(obj.protocol) && 443 == obj.port)) {
if ((/(http|ws)/.test(obj.protocol) && 80 == obj.port) ||
(/(http|ws)s/.test(obj.protocol) && 443 == obj.port)) {
delete obj.port;
}

obj.path = obj.path || '/';

// define unique id
obj.id = obj.protocol + obj.hostname + (obj.port ? (':' + obj.port) : '');
obj.id = obj.protocol + obj.host + (obj.port ? (':' + obj.port) : '');

// define href
obj.href = obj.protocol + '://' + obj.host + (obj.port ? (':' + obj.port) : '');

return obj;
}
Loading

0 comments on commit fc4cdb4

Please sign in to comment.