网络设置和处理连接

时间:2015-11-30 11:19:07

标签: java netty

我不确定我是否为我的需要设置了正确的引导程序。

到目前为止,我有这个(即2个客户):

服务器仅显示以下内容:

REGISTER

完整

添加了1个玩家

[服务器] - /127.0.0.1:51850已加入!

在线玩家:1

REGISTER

完整

添加了2个玩家

[服务器] - /127.0.0.1:51869已加入!

在线玩家:2

当它还应显示来自每个客户的“Alo ...”

客户端仅在下一个客户端连接时才显示来自服务器的数据(卡正在其他功能中设置并正确构建)。首先(如果只有1个客户端)/最后一个客户端显示任何内容。 我希望在客户端连接时立即在每个客户端显示卡而不是等待下一个客户端。

设置服务器:

EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
                    bootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChatServerInitializer())
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childOption(ChannelOption.TCP_NODELAY, true);

                    ChannelFuture f = bootstrap.bind(port).sync();
                     f.channel().closeFuture().sync();
            // bootstrap.bind(port).sync().channel().closeFuture().sync();
        }
        finally { 
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

ChatServerInitializer:

protected void initChannel(SocketChannel arg0) throws Exception {
        ChannelPipeline pipeline = arg0.pipeline();

        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());

        pipeline.addLast("handler", new ChatServerHandler());
    }
}

ChatServerHandler:

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

private String cards = "";

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
                System.out.println("REGISTER");
        }
    @Override
    public void channelActive(final ChannelHandlerContext ctx) throws Exception{
        Channel incoming = ctx.channel();

        final ChannelFuture f = incoming.writeAndFlush(cards);
        f.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
               System.out.println("Complete");
            }
        });

        for(Channel channel : channels) {
            channel.writeAndFlush("[Server] - " + incoming.remoteAddress() + " has joined!\n");

        }

        channels.add(ctx.channel());
        showMessage( "[Server] - " + incoming.remoteAddress().toString() + " has joined!" );
        showMessage( "Players online: " + channels.size() );
        System.out.println("Added " + channels.size() + " player");
    }

    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        for(Channel channel : channels) {
            channel.writeAndFlush("[Server] - " + incoming.remoteAddress() + " has left!\n");
        }
        showMessage( "[Server] - " + incoming.remoteAddress().toString() + " has left!" );
        channels.remove(ctx.channel());
        showMessage( "Players online: " + channels.size() );

    }
    @Override
    public void channelRead0(ChannelHandlerContext arg0, String arg1) throws Exception {
        System.out.println(arg1);
    }

    }

设置客户端:

EventLoopGroup group = new NioEventLoopGroup();

        try{
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChatClientInitializer());
        //  bootstrap.connect(host,port).sync().channel();
            ChannelFuture f = bootstrap.connect(host, port).sync();
            f.channel().closeFuture().sync();
        }
        finally {
            group.shutdownGracefully();
        }

ChatClientInitializer:

@Override
    protected void initChannel(SocketChannel arg0) throws Exception {

        ChannelPipeline pipeline = arg0.pipeline();

        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());

        pipeline.addLast("handler", new ChatClientHandler());
    }

ChatClientHandler:

public void channelActive(ChannelHandlerContext ctx){
        final ChannelFuture f = ctx.writeAndFlush("Alo...");
        f.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
               System.out.println("Completed..");
            }
        });
    }

    public void channelRead0(ChannelHandlerContext arg0, String arg1) throws Exception{
            System.out.println(arg1);
            showMessage(arg1);
    }

我最终想要的是: 将8个客户端连接到服务器。每个客户应该从服务器,相同的5张卡(卡)和每2张不同的卡(手)获得。就像德州扑克一样。然后,如果客户下注/检查/折叠并与每个响应进行交互,则服务器应该收听。 那个引导程序设置好吗?我应该在客户端/服务器处理程序中执行所有这些代码吗?

0 个答案:

没有答案