ルーム内のクライアントに特定のプロパティが関連付けられているかどうかを調べようとしています。socket.io get メソッドの非同期性が問題を引き起こしています。必要なもののように見える非同期ライブラリを見てきましたが、それをこの状況に適用する方法を視覚化するのが困難です。
get が非同期ではないと仮定して、関数をどのように機能させたいかを次に示します。
/**
*
**/
socket.on('disconnect', function(data) {
socket.get('room', function(err, room) {
if(!roomHasProperty(room)) {
io.sockets.in(room).emit('status', { message: 'property-disconnect' });
}
});
});
/**
*
**/
var roomClients = function(room) {
var _clients = io.sockets.clients(room);
return _clients;
}
/**
*
**/
var roomHasProperty = function(room) {
// get a list of clients in the room
var _clients = roomClients(room);
// build up an array of tasks to be completed
var tasks = [];
// loop through each socket in the room
for(key in _clients) {
var _socket = _clients[key];
// grab the type from the sockets data store and check for a control type
_socket.get('type', function (err, type) {
// ah crap, you already went ahead without me!?
if(type == 'property') {
// found a the property type we were looking for
return true;
}
});
}
// didn't find a control type
return false;
}
これを行うより良い方法はありますか?