簡體   English   中英

ChannelGroup無法按Netty 4.1.6的預期工作

[英]ChannelGroup not working as intended in Netty 4.1.6

我有一個ChannelGroup含4個客戶機已經在逐一記錄和已被添加到該組,因為他們通過Netty的方法登錄handlerAdded

static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); 

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    channels.add(ctx.channel());
    assignGroup(ctx.channel());
}

稍后,我將ChannelGroup保存在一個名為GameMaster的對象中,並初始化一個游戲循環,使每個客戶都有機會ChannelGroup游戲:

    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); 被發送到另一個客戶端(而不是第一個客戶端,因為它應該通過循環發送)!

我想念什么嗎?

“第一個”是什么意思? ChannelGroup是一個Set,它不能保證在迭代時保持插入順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM