Skip to content

Commit

Permalink
Added sweet error message when trying to use flashsocket without root…
Browse files Browse the repository at this point in the history
… privileges (thanks @wink for inspiration)

Removed clients/clientsIndex and only using the index (fixes #28)
  • Loading branch information
rauchg committed Aug 31, 2010
1 parent 24b2714 commit ab623c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
23 changes: 8 additions & 15 deletions lib/socket.io/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ Listener = module.exports = function(server, options){
require('sys').log(message);
}
}, options);
this.clients = [];
this.clientsIndex = {};

this.clients = {};

var listeners = this.server.listeners('request');
this.server.removeAllListeners('request');
Expand Down Expand Up @@ -65,9 +65,9 @@ sys.inherits(Listener, process.EventEmitter);
for (var i in options) Listener.prototype[i] = options[i];

Listener.prototype.broadcast = function(message, except){
for (var i = 0, l = this.clients.length; i < l; i++){
if (this.clients[i] && (!except || [].concat(except).indexOf(this.clients[i].sessionId) == -1)){
this.clients[i].send(message);
for (var i = 0, k = Object.keys(this.clients), l = k.length; i < l; i++){
if (this.clients[k[i]] && (!except || [].concat(except).indexOf(this.clients[k[i]].sessionId) == -1)){
this.clients[k[i]].send(message);
}
}
return this;
Expand All @@ -79,7 +79,7 @@ Listener.prototype.check = function(req, res, httpUpgrade, head){
parts = path.substr(1).split('/');
if (!(parts[1] in transports)) return false;
if (parts[2]){
cn = this._lookupClient(parts[2]);
cn = this.clients[parts[2]];
if (cn){
cn._onConnect(req, res);
} else {
Expand All @@ -94,17 +94,11 @@ Listener.prototype.check = function(req, res, httpUpgrade, head){
return false;
};

Listener.prototype._lookupClient = function(sid){
return this.clientsIndex[sid];
};

Listener.prototype._onClientConnect = function(client){
if (!(client instanceof Client) || !client.sessionId){
return this.options.log('Invalid client');
}
client.i = this.clients.length;
this.clients.push(client);
this.clientsIndex[client.sessionId] = client;
this.clients[client.sessionId] = client;
this.options.log('Client '+ client.sessionId +' connected');
this.emit('clientConnect', client);
this.emit('connection', client);
Expand All @@ -115,8 +109,7 @@ Listener.prototype._onClientMessage = function(data, client){
};

Listener.prototype._onClientDisconnect = function(client){
this.clientsIndex[client.sessionId] = null;
this.clients[client.i] = null;
delete this.clients[client.sessionId];
this.options.log('Client '+ client.sessionId +' disconnected');
this.emit('clientDisconnect', client);
};
Expand Down
21 changes: 20 additions & 1 deletion lib/socket.io/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ try {
socket.write('</cross-domain-policy>\n');
socket.end();
}).listen(843);
} catch(e){}
} catch(e){
if (e.errno == 13){
console.error("\x1B[1;31m" + [
'================================================',
'| WARNING! DANGER! |',
'| |',
'| The flash websocket transport will not work |',
'| unless you run your node instance with root |',
'| privileges. |',
'| |',
'| A flash XML policy file has to be served on |',
'| port 843 (privileged) for it to work. |',
'| |',
'| You can run socket.io without this trasnport |',
'| to make this message go (refer to README). |',

This comment has been minimized.

Copy link
@TooTallNate

TooTallNate Aug 31, 2010

Contributor

to make this message go away?

This comment has been minimized.

Copy link
@tj

tj Aug 31, 2010

Contributor

haha this is sweet BAM

This comment has been minimized.

Copy link
@rauchg

rauchg Sep 1, 2010

Author Contributor

@TooTallNate I thought I could get away with that, didn't want to resize the box :P

'| |',
'===============================================|'
].join("\n") + "\x1B[0m");
}
}

0 comments on commit ab623c3

Please sign in to comment.