У меня есть ChannelGroup
, содержащий 4 клиента, которые вошли в систему один за другим и были добавлены в группу при входе в систему по методу Netty handlerAdded
.ChannelGroup не работает, как предполагалось, в Netty 4.1.6
static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
channels.add(ctx.channel());
assignGroup(ctx.channel());
}
позже я сохранить ChannelGroup
в объекте с именем GameMaster
и инициализировать цикл игры, которая дает каждому клиенту возможность сыграть свою очередь:
static ChannelGroup channels; // contains the actual channels, saved in contructor
public void bettingPhase(boolean isOk) {
int i = 0;
int y = 0;
for (Channel chan : channels) { // Should start on the first client that logged in
Server.MyMessage.Builder message = Server.MyMessage.newBuilder();
message.setKeyword("304");
message.setValue("Please input a contract:");
chan.writeAndFlush(message); // PROBLEM HERE
while (isOk == false) { // Loop to check the playing client's input
Server.MyMessage.Builder message2 = Server.MyMessage.newBuilder();
try {
Thread.sleep(1000);
if (playerAnswer.getKeyword().equals("CONTRACT")) {
System.out.println("Player has inputed contract.");
i++;
message2.setKeyword("310");
chan.writeAndFlush(message); // Tells the current client his turn is over
System.out.println("End of turn for player.");
for (Channel channel : channels) { // Loop to tell the next client it's his turn
if (y == i) {
message2.setKeyword("309");
channel.writeAndFlush(message);
System.out.println("Start of turn for player.");
}
y++;
}
isOk = true;
playerAnswer.clearKeyword();
}
} catch (Exception e) {
e.printStackTrace();
}
}
isOk = false;
}
}
Пожалуйста, извините этот большой кусок кода.
Но почему-то мое предложение "304 Please input a contract:"
, отправленное через chan.writeAndFlush(message);
, отправляется другому клиенту (а не по первому, как это должно быть через цикл)!
Я что-то упустил?
Я этого не знал, теперь я сохранил их в «Канале []», чтобы получить к ним доступ, как я хочу. –