在游戏中,好友和聊天系统里,需要用到玩家的在线状态,搞了两三天,大致搞明白了,在这里总结一下.
pomelo的API中有一个方法:BackendSessionService.getByUid(frontendId,uid,cb);
frontendId: global.app.getServersByType('connector);这是一个数组,里面是前段服务器配置信息,我们要用到的时数组中得id.
uid:玩家账户ID 不是角色ID.
cb:function(err,backendSessions){
//backendSessions是我们想要获取的服务器端的session.
}
var player = {id:1,name:'li',uid:1001};
var async = require('async');
var uid = player.uid;//玩家账户ID
var backendSessionService = global.app.get('backendSessionService');
var connectors = global.app.getServersByType('connector);//获取前段服务器
async.mapSeries(connectors,function(connector,callback){
backendSessionService.getByUid(connector.id,uid,function(err,sessions){
if(!!sessions){
var session = sessions[0];//一个角色只有一个session
player.online = 0;//默认离线
if(session.uid == uid){
player.online = 1;//在线
}
callback(err,player);
}else{
callback(err,player);
}
});
},function(err,result){
console.log('result:',result);//{id:1,name:'li',uid:1001,online:1}
});