0
我開始使用Netty,但還沒有想到如何設置我的協議。多個遊戲會話
我想要實現的是在一臺服務器上處理多個遊戲會話。
ChannelGroup
這個類對我的場景真的很有幫助,但我想知道如何在例如同時在列表上發出多個操作的情況下設置一個遊戲會話列表,而不需要服務器擲出ConcurrentModificationException
。
public class GameSession {
int id;
ChannelGroup channels;
}
public class MyServerHandler extends ChannelHandlerAdapter{
private List<GameSession> sessions;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if(msg.equals(CREATE_ROOM))
{
sessions.add(new GameSession());
}
else if(msg.equals(JOIN_ROOM))
{
// look for specific room...
// loop here can collide with the above block and throw ConcurrentModificationException
}
}
}
我以正確的方式思考它嗎?我怎樣才能實現這樣的行爲?
ChannelGroup是線程安全的,但List對象不是。嘗試使用Collections.synchronizedList()。 http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29 – jgitter
爲每個遊戲會話你將有幾個頻道? – HCarrasko
@Hector是的。每個會話都有自己的ChannelGroup –