我用play框架和akka编写websocket游戏。我尝试从其经理中删除玩家。 Player
actor表示具有连接的玩家状态。
public class Player extends UntypedActor {
private final String openId;
private final ActorRef out;
public Player(String openId, ActorRef out) {
this.openId = openId;
this.out = out;
}
@Override
public void preStart() throws Exception {
Application.clientManager.tell(
new ClientsManager.PlayerConnectedMessage(openId, self()), self()
);
}
@Override
public void postStop() throws Exception {
//may be tell my id here?
}
}
创建时,玩家在prestart
的经理演员中注册。当连接关闭时,Player
会破坏。我需要从经理
public class ClientManager extends UntypedActor {
private static Map<String, ActorRef> clients = new HashMap<>();
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof PlayerConnectedMessage) {
final PlayerConnectedMessage connectedMessage = (PlayerConnectedMessage) message;
context().watch(connectedMessage.client);
clients.put(
connectedMessage.id,
connectedMessage.client
);
} else if (message instanceof Terminated) {
final ActorRef toDelete = ((Terminated) message).actor();
//How remove, i have only ActorRef
clients.remove(...);
}
}
public static class PlayerConnectedMessage {
private final String id;
private final ActorRef client;
public PlayerConnectedMessage(String id, ActorRef client) {
this.id = id;
this.client = client;
}
}
}
在我的架构中,我想要商店客户Map<String, ActorRef>
,其中String
- 来自may逻辑。因此,我无法在Terminated
消息中移除客户端 - 我只有ActorRef
。
可能的解决方案:向Disconnect
Player
postStop
的管理员发送watch
消息,并且不再使用postStop()
。
是否有一个优雅的解决方案&#39; watch&#39;方式,或categoriesS
的解决方案也很好。我担心可能的客户泄漏。