Skip to content

Commit ab623c3

Browse files
committed
Added sweet error message when trying to use flashsocket without root privileges (thanks @wink for inspiration)
Removed clients/clientsIndex and only using the index (fixes #28)
1 parent 24b2714 commit ab623c3

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

lib/socket.io/listener.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ Listener = module.exports = function(server, options){
3535
require('sys').log(message);
3636
}
3737
}, options);
38-
this.clients = [];
39-
this.clientsIndex = {};
38+
39+
this.clients = {};
4040

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

6767
Listener.prototype.broadcast = function(message, except){
68-
for (var i = 0, l = this.clients.length; i < l; i++){
69-
if (this.clients[i] && (!except || [].concat(except).indexOf(this.clients[i].sessionId) == -1)){
70-
this.clients[i].send(message);
68+
for (var i = 0, k = Object.keys(this.clients), l = k.length; i < l; i++){
69+
if (this.clients[k[i]] && (!except || [].concat(except).indexOf(this.clients[k[i]].sessionId) == -1)){
70+
this.clients[k[i]].send(message);
7171
}
7272
}
7373
return this;
@@ -79,7 +79,7 @@ Listener.prototype.check = function(req, res, httpUpgrade, head){
7979
parts = path.substr(1).split('/');
8080
if (!(parts[1] in transports)) return false;
8181
if (parts[2]){
82-
cn = this._lookupClient(parts[2]);
82+
cn = this.clients[parts[2]];
8383
if (cn){
8484
cn._onConnect(req, res);
8585
} else {
@@ -94,17 +94,11 @@ Listener.prototype.check = function(req, res, httpUpgrade, head){
9494
return false;
9595
};
9696

97-
Listener.prototype._lookupClient = function(sid){
98-
return this.clientsIndex[sid];
99-
};
100-
10197
Listener.prototype._onClientConnect = function(client){
10298
if (!(client instanceof Client) || !client.sessionId){
10399
return this.options.log('Invalid client');
104100
}
105-
client.i = this.clients.length;
106-
this.clients.push(client);
107-
this.clientsIndex[client.sessionId] = client;
101+
this.clients[client.sessionId] = client;
108102
this.options.log('Client '+ client.sessionId +' connected');
109103
this.emit('clientConnect', client);
110104
this.emit('connection', client);
@@ -115,8 +109,7 @@ Listener.prototype._onClientMessage = function(data, client){
115109
};
116110

117111
Listener.prototype._onClientDisconnect = function(client){
118-
this.clientsIndex[client.sessionId] = null;
119-
this.clients[client.i] = null;
112+
delete this.clients[client.sessionId];
120113
this.options.log('Client '+ client.sessionId +' disconnected');
121114
this.emit('clientDisconnect', client);
122115
};

lib/socket.io/transports/flashsocket.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ try {
3636
socket.write('</cross-domain-policy>\n');
3737
socket.end();
3838
}).listen(843);
39-
} catch(e){}
39+
} catch(e){
40+
if (e.errno == 13){
41+
console.error("\x1B[1;31m" + [
42+
'================================================',
43+
'| WARNING! DANGER! |',
44+
'| |',
45+
'| The flash websocket transport will not work |',
46+
'| unless you run your node instance with root |',
47+
'| privileges. |',
48+
'| |',
49+
'| A flash XML policy file has to be served on |',
50+
'| port 843 (privileged) for it to work. |',
51+
'| |',
52+
'| You can run socket.io without this trasnport |',
53+
'| to make this message go (refer to README). |',
54+
'| |',
55+
'===============================================|'
56+
].join("\n") + "\x1B[0m");
57+
}
58+
}

0 commit comments

Comments
 (0)