Skip to content

Commit

Permalink
Merge pull request #218 from 3rd-Eden/namespace.auth
Browse files Browse the repository at this point in the history
Namespace.auth
  • Loading branch information
rauchg committed Jun 29, 2011
2 parents 06052d2 + 1966441 commit 27be7ac
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 35 deletions.
6 changes: 5 additions & 1 deletion lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@
if (packet.advice){
this.socket.onError(packet);
} else {
this.$emit('error', packet.reason);
if (packet.reason == 'unauthorized') {
this.$emit('connect_failed', packet.reason);
} else {
this.$emit('error', packet.reason);
}
}
break;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
* @api private
*/

Socket.prototype.publish = function(){
Socket.prototype.publish = function () {
this.emit.apply(this, arguments);

var nsp;
Expand Down Expand Up @@ -219,7 +219,7 @@
if (self.options.connectTimeout) {
self.connectTimeoutTimer = setTimeout(function () {
if (!self.connected) {
if (self.options['try multiple transports']){
if (self.options['try multiple transports']) {
if (!self.remainingTransports) {
self.remainingTransports = self.transports.slice(0);
}
Expand Down Expand Up @@ -343,7 +343,7 @@
* @api private
*/

Socket.prototype.onConnect = function(){
Socket.prototype.onConnect = function () {
this.connected = true;
this.connecting = false;
if (!this.doBuffer) {
Expand Down Expand Up @@ -390,8 +390,8 @@
*/

Socket.prototype.onError = function (err) {
if (err && err.advice){
if (err.advice === 'reconnect'){
if (err && err.advice) {
if (err.advice === 'reconnect') {
this.disconnect();
this.reconnect();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @api private
*/

Transport.prototype.onData = function(data){
Transport.prototype.onData = function (data) {
this.clearCloseTimeout();
this.setCloseTimeout();

Expand Down Expand Up @@ -69,7 +69,7 @@
return this.onHeartbeat();
}

if (packet.type == 'connect' && packet.endpoint == ''){
if (packet.type == 'connect' && packet.endpoint == '') {
this.onConnect();
}

Expand Down
32 changes: 23 additions & 9 deletions lib/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
* @api public
*/

Flashsocket.prototype.open = function(){
Flashsocket.prototype.open = function () {
var self = this, args = arguments;
WebSocket.__addTask(function(){
WebSocket.__addTask(function () {
io.Transport.websocket.prototype.open.apply(self, args);
});
return this;
Expand All @@ -69,14 +69,27 @@
* @api public
*/

Flashsocket.prototype.send = function(){
Flashsocket.prototype.send = function () {
var self = this, args = arguments;
WebSocket.__addTask(function(){
WebSocket.__addTask(function () {
io.Transport.websocket.prototype.send.apply(self, args);
});
return this;
};

/**
* Disconnects the established `Flashsocket` connection.
*
* @returns {Transport}
* @api public
*/

Flashsocket.prototype.close = function () {
WebSocket.__tasks.length = 0;
io.Transport.websocket.prototype.close.call(this);
return this;
};

/**
* Check if the Flashsocket transport is supported as it requires that the Adobe
* Flash Player plugin version `10.0.0` or greater is installed. And also check if
Expand All @@ -86,7 +99,7 @@
* @api public
*/

Flashsocket.check = function(socket){
Flashsocket.check = function (socket) {
if (
typeof WebSocket == 'undefined'
|| !('__initialize' in WebSocket) || !swfobject
Expand All @@ -104,13 +117,14 @@

// Only start downloading the swf file when the checked that this browser
// actually supports it
if (supported){
if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined'){
if (supported && !Flashsocket.loaded) {
if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined') {
// Set the correct file based on the XDomain settings
WEB_SOCKET_SWF_LOCATION = path.join('/');
}

WebSocket.__initialize();
Flashsocket.loaded = true;
}

return supported;
Expand All @@ -125,15 +139,15 @@
* @api public
*/

Flashsocket.xdomainCheck = function(){
Flashsocket.xdomainCheck = function () {
return true;
};

/**
* Disable AUTO_INITIALIZATION
*/

if (typeof window != 'undefined'){
if (typeof window != 'undefined') {
WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/transports/jsonp-polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@

script.async = true;
script.src = this.prepareUrl() + '/?t=' + (+new Date) + '&i=' + this.index;
script.onerror = function(){
script.onerror = function () {
self.onClose();
};

Expand Down
25 changes: 13 additions & 12 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
exports.websocket = WS;

/**
* The WebSocket transport uses the HTML5 WebSocket API to establish an persistent
* connection with the Socket.IO server. This transport will also be inherited by the
* FlashSocket fallback as it provides a API compatible polyfill for the WebSockets.
* The WebSocket transport uses the HTML5 WebSocket API to establish an
* persistent connection with the Socket.IO server. This transport will also
* be inherited by the FlashSocket fallback as it provides a API compatible
* polyfill for the WebSockets.
*
* @constructor
* @extends {io.Transport}
Expand Down Expand Up @@ -49,7 +50,7 @@
* @api public
*/

WS.prototype.open = function(){
WS.prototype.open = function () {
this.websocket = new WebSocket(this.prepareUrl());

var self = this;
Expand All @@ -72,8 +73,8 @@
};

/**
* Send a message to the Socket.IO server. The message will automatically be encoded
* in the correct message format.
* Send a message to the Socket.IO server. The message will automatically be
* encoded in the correct message format.
*
* @returns {Transport}
* @api public
Expand Down Expand Up @@ -104,7 +105,7 @@
* @api public
*/

WS.prototype.close = function(){
WS.prototype.close = function () {
this.websocket.close();
return this;
};
Expand All @@ -117,7 +118,7 @@
* @api private
*/

WS.prototype.onError = function(e){
WS.prototype.onError = function (e) {
this.socket.onError(e);
};

Expand All @@ -126,8 +127,8 @@
*
* @api private
*/
WS.prototype.scheme = function(){
return (this.socket.options.secure ? 'wss' : 'ws');
WS.prototype.scheme = function () {
return this.socket.options.secure ? 'wss' : 'ws';
};

/**
Expand All @@ -138,7 +139,7 @@
* @api public
*/

WS.check = function(){
WS.check = function () {
return 'WebSocket' in window && !('__addTask' in WebSocket);
};

Expand All @@ -149,7 +150,7 @@
* @api public
*/

WS.xdomainCheck = function(){
WS.xdomainCheck = function () {
return true;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/transports/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
* @api public
*/

XHR.xdomainCheck = function(){
XHR.xdomainCheck = function () {
return XHR.check(null, true);
};

Expand Down
8 changes: 4 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@
* @api public
*/

util.merge = function merge(target, additional, deep, lastseen){
util.merge = function merge (target, additional, deep, lastseen) {
var seen = lastseen || []
, depth = typeof deep == 'undefined' ? 2 : deep
, prop;

for (prop in additional){
if (additional.hasOwnProperty(prop) && util.indexOf(seen, prop) < 0){
if (typeof target[prop] !== 'object' || !depth){
for (prop in additional) {
if (additional.hasOwnProperty(prop) && util.indexOf(seen, prop) < 0) {
if (typeof target[prop] !== 'object' || !depth) {
target[prop] = additional[prop];
seen.push(additional[prop]);
} else {
Expand Down
8 changes: 8 additions & 0 deletions support/test-runner/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ suite('socket.test.js', function () {
io.of('/b').on('connection', function (socket) {});
});

server('test authorizing for namespaces', function (io) {
io.of('/a')
.authorization(function (data, fn) {
fn(null, false);
})
.on('connection', function (socket) {});
});

server('test sending json from server', function (io) {
io.sockets.on('connection', function (socket) {
io.sockets.json.send(3141592);
Expand Down
17 changes: 17 additions & 0 deletions test/socket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@
});
},

'test authorizing for namespaces': function (next) {
var socket = create().socket

function finish () {
socket.of('').disconnect();
next();
};

socket.of('/a')
.on('connect_failed', function (msg) {
next();
})
.on('error', function (msg) {
throw new Error(msg || 'Received an error');
});
},

'test sending json from server': function (next) {
var socket = create();

Expand Down

0 comments on commit 27be7ac

Please sign in to comment.