Skip to content

Instantly share code, notes, and snippets.

@tily
Created June 21, 2012 13:52
Show Gist options
  • Save tily/2965857 to your computer and use it in GitHub Desktop.
Save tily/2965857 to your computer and use it in GitHub Desktop.
socket.io ネームスペース認証時 callback の挙動を調べる
$ node socket_io_namespace_authorization_test.js localhost 1234
info - socket.io started
/null_true への接続を試行しています
socket 全体で connect を受信しました
名前空間 /null_true で connect を受信しました (理由:undefined)
----------------------------------------------------------------------
/null_false への接続を試行しています
socket 全体で connect を受信しました
名前空間 /null_false で connect_failed を受信しました (理由:unauthorized)
----------------------------------------------------------------------
/ascii_true への接続を試行しています
socket 全体で connect を受信しました
名前空間 /ascii_true で error を受信しました (理由:)
----------------------------------------------------------------------
/ascii_false への接続を試行しています
socket 全体で connect を受信しました
名前空間 /ascii_false で error を受信しました (理由:)
----------------------------------------------------------------------
/unauthorized_true への接続を試行しています
socket 全体で connect を受信しました
名前空間 /unauthorized_true で connect_failed を受信しました (理由:unauthorized)
----------------------------------------------------------------------
/unauthorized_false への接続を試行しています
socket 全体で connect を受信しました
名前空間 /unauthorized_false で connect_failed を受信しました (理由:unauthorized)
----------------------------------------------------------------------
/multibyte_true への接続を試行しています
socket 全体で connect を受信しました
名前空間 /multibyte_true で error を受信しました (理由:)
----------------------------------------------------------------------
/multibyte_false への接続を試行しています
socket 全体で connect を受信しました
名前空間 /multibyte_false で error を受信しました (理由:)
----------------------------------------------------------------------
/ascii への接続を試行しています
socket 全体で connect を受信しました
名前空間 /ascii で error を受信しました (理由:)
----------------------------------------------------------------------
/multibyte への接続を試行しています
socket 全体で connect を受信しました
名前空間 /multibyte で error を受信しました (理由:)
----------------------------------------------------------------------
/unauthorized への接続を試行しています
socket 全体で connect を受信しました
名前空間 /unauthorized で connect_failed を受信しました (理由:unauthorized)
----------------------------------------------------------------------
// Usage: node socket_io_namespace_authorization_test.js localhost 3000
var host = process.argv[2]
, port = process.argv[3]
, server = require('socket.io').listen(parseInt(port))
, client = require('socket.io-client')
, Deferred = require('jsdeferred').Deferred
, config = {
"/null_true" : [null, true ],
"/null_false" : [null, false],
"/ascii_true" : ['ascii', true ],
"/ascii_false" : ['ascii', false],
"/unauthorized_true" : ['unauthorized', true ],
"/unauthorized_false" : ['unauthorized', false],
"/multibyte_true" : ['マルチバイト', true ],
"/multibyte_false" : ['マルチバイト', false],
"/ascii" : ['ascii'],
"/multibyte" : ['マルチバイト'],
"/unauthorized" : ['unauthorized']
}
;
server.configure(function() {
server.set('log level', 0);
});
// Servers
for(var ns in config) {
var authorization = function() {
var _ns = ns;
return function(handshakeData, callback) {
if(config[_ns][1]) {
callback(config[_ns][0], config[_ns][1]);
} else {
callback(config[_ns][0]);
}
}
};
server.of(ns).authorization(authorization());
}
// Clients
var url = ['http://', host, ':', port].join('');
var nsList = []; for(var ns in config) { nsList.push(ns) };
Deferred.loop(nsList.length, function(i) {
var ns = nsList[i];
var deferred = new Deferred();
var socket = client.connect(url, {'force new connection': true});
var nsSocket = socket.of(ns);
console.log(ns + " への接続を試行しています");
['connect', 'error', 'connect_failed'].forEach(function(e) {
socket.on(e, function() {
console.log('socket 全体で ' + e + ' を受信しました');
});
nsSocket.on(e, function(reason) {
console.log('名前空間 ' + ns + ' で ' + e + ' を受信しました (理由:' + reason + ')');
console.log('----------------------------------------------------------------------');
deferred.call();
});
})
return deferred;
})
.next(function() {
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment