为了业务需要,在某个用户离开时,需要通知其他玩家,所以重写了left方法,请问是不是因为handler在left之前移除,所以消息发不出去了?
//重写父类的left(room)方法 ,离开的同时发送消息给其他人:我要离开了
@Override
public void left(String room){
super.left(room);
sendLeft2Others(room);
}
private void sendLeft2Others(String room2) {
// TODO Auto-generated method stub
//如果游戏中掉线不移除该玩家的所有缓存user和room.usersInGame
if(redisService.get(roomPrefix+"openId:"+myOpenId) != null){
NutMap resp = new NutMap();
User me = Json.fromJson(User.class,redisService.get(roomPrefix+"openId:"+myOpenId));
Room r = Json.fromJson(Room.class,redisService.get(me.getRoomId()));
Set<String> openIdsInRoom = r.getOpenIdsInRoom();
if(openIdsInRoom != null){
Iterator<String> iter = openIdsInRoom.iterator();
//移除room里面的UsersInRoom的缓存
while(iter.hasNext()){
String openIdInRoom = iter.next();
if(openIdInRoom.equals(roomPrefix+"openId:"+myOpenId)){
iter.remove();
System.out.println("玩家离线,从openIdsInRoom列表移除");
break;
}
}
}
redisService.set(me.getRoomId(), r.toString());
if(me.getScore() == null){//分数如果为空则说明没有参与游戏,移除该玩家openId-->User缓存
resp.setv("action", "left");
//移除user对象的缓存
redisService.del(roomPrefix+"openId:"+myOpenId);
System.out.println("有位玩家离开,该openId缓存已移除");
}else{
resp.setv("action", "disconnect");
}
//通知其他玩家不包括自己
resp.setv("msg", myOpenId);
sendAllNoMe(resp, roomPrefix+room, myOpenId);
}
}