Page History
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Override
public AbstractActor.Receive createReceive() {
return receiveBuilder()
.match(ConnectInfo.class, c -> {
if(c.getCmd()== ConnectInfo.Cmd.CONNECT){
sessionMgr.put(c.getSessionId(),c.getWsSender());
log.info("user connected:"+c.getSessionId());
}else if(c.getCmd()== ConnectInfo.Cmd.DISCONET){
sessionMgr.remove(c.getSessionId());
Player removeUser = new Player();
removeUser.setSession(c.getSessionId());
if(c.getTableNo()>0){
findTableByID(c.getTableNo()).tell(new SeatOut(removeUser),ActorRef.noSender());
}else{
findTableALL().tell(new SeatOut(removeUser),ActorRef.noSender());
}
log.info("user disconnected:"+c.getSessionId());
}
sessionMgr.put(c.getSessionId(),c.getWsSender());
})
.match(TableCreate.class, t->{
// Create a table under the lobby, if you have an Actor named TableManagement, you can move easily.
String tableUID = "table-" + t.getTableId();
if(t.getCmd() == TableCreate.Cmd.CREATE){
ActorRef tableActor = getContext().actorOf( TableActor.props(t,this.getSelf() ), tableUID);
tableActor.tell(t,ActorRef.noSender());
}
})
.match(JoinGame.class, j->{
joinGameTable(j.getTableId(),j.getName(),j.getSession());
})
.match(MessageWS.class, m->{
send(m.getSession(),m.getGameMessage());
})
.build();
} |
코드는 조금더 복잡해 집니다. 물론 다이어그램과 코드가 일치가 안될수도 있지만
메시지 흐름을 다이어그램과 일치시키는것은 쉬운일은 아닙니다. 자신의 방식으로 다이어그램으로 먼저 정리 해보는것은 정리하여 일치시키려는 노력은 중요합니다.
게임로직은 API가 단순하게 DB를 저장시키고 조회를 하는것이 아니기때문입니다아니기 때문입니다.
ActorPath를 통한 메시지 전송
| Code Block | ||||
|---|---|---|---|---|
| ||||
private ActorRef findTableByID(int tableID) throws Exception {
String tableActorPath = "/user/lobby/table-"+tableID;
ActorSelection tableSelect = this.getContext().actorSelection(tableActorPath);
FiniteDuration duration = FiniteDuration.create(1, TimeUnit.SECONDS);
Future<ActorRef> fut = tableSelect.resolveOne(duration);
ActorRef tableActor = Await.result(fut, duration);
return tableActor;
} |
...
- https://doc.akka.io/docs/akka/2.5/general/addressing.html - Actor Path
- https://www.baeldung.com/akka-with-spring
기타 참고자료
또한 메시지 처리에서 Thread와 Actor를 비교해보는것은 , 메시징 처리를 위한 좋은 학습자료가 될것입니다.
...