zoukankan      html  css  js  c++  java
  • 学习了半个多月的TankGame

    public class Server {
        public static ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    
        public void serverStart() {
            /** 负责处理连接的 group 一般只需要一个线程 */
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            /** 负责发送与接收消息的 group 具体几个现场看情况 */
            EventLoopGroup workerGroup = new NioEventLoopGroup(2);
    
            try {
                ServerBootstrap b = new ServerBootstrap();
                ChannelFuture f = b.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline()
                                .addLast(new MsgEncode())
                                .addLast(new MsgDecode())
                                .addLast(new ServerChildHandler());
                            }
                        })
                        .bind(8888)
                        .sync();
    
                ServerFrame.INSTANCE.updateServerMsg("server started!");
    
                f.channel().closeFuture().sync(); //close()->ChannelFuture
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                workerGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }
        }
    
    
    }
    
    class ServerChildHandler extends ChannelInboundHandlerAdapter { //SimpleChannleInboundHandler Codec
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            Server.clients.add(ctx.channel());
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println(msg);
            ServerFrame.INSTANCE.updateClientMsg(msg.toString());
            Server.clients.writeAndFlush(msg);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            Server.clients.remove(ctx.channel());
            ctx.close();
        }
    
    
    }

    client

    public class Client {
        public static final Client INSTANCE = new Client();
        private Channel channel = null;
    
        private Client() {}
        public void connect() {
            EventLoopGroup group = new NioEventLoopGroup(1);
            Bootstrap b = new Bootstrap();
            try {
                ChannelFuture f = b.group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer())
                        .connect("localhost", 8888);
                f.addListener((ChannelFuture future)-> {
                    if (!future.isSuccess()) {
                        System.out.println("not connected!");
                    } else {
                        System.out.println("connected!");
                        // initialize the channel
                        channel = future.channel();
                    }
                });
                f.sync();
                // wait until close
                f.channel().closeFuture().sync();
                System.out.println("connection closed!");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                group.shutdownGracefully();
            }
        }
        public void send(Msg msg) {
            channel.writeAndFlush(msg);
        }
    }
    
    class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
    
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline()
                    .addLast(new MsgEncode())
                    .addLast(new MsgDecode())
                    .addLast(new ClientHandler());
        }
    
    }
    
    class ClientHandler extends SimpleChannelInboundHandler<Msg> {
    
        @Override
        public void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
           msg.handle();
        }
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            TankJoinMsg tankJoinMsg = new TankJoinMsg(GameModel.getInstance().getMainTank());
            System.out.println("发送消息到服务器 : "+tankJoinMsg);
            ctx.writeAndFlush(tankJoinMsg);
        }
    
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            super.userEventTriggered(ctx, evt);
            //如果消息是心跳包
            if(evt instanceof IdleStateEvent){
        
            }
        }
    }
  • 相关阅读:
    在一台服务器上搭建相对高可用HiveServer实践
    HDU 4462 Scaring the Birds (暴力求解,二进制法)
    HDU 4461 The Power of Xiangqi (水题)
    HDU 4460 Friend Chains (BFS,最长路径)
    UVaLive 7270 Osu! Master (统计)
    CodeForces 705C Thor (模拟+STL)
    CodeForces 705B Spider Man (水题)
    CodeForces 705A Hulk (水题)
    UVa 11971 Polygon (数学,转化)
    UVa 10900 So you want to be a 2n-aire? (概率DP,数学)
  • 原文地址:https://www.cnblogs.com/self-crossing/p/11011703.html
Copyright © 2011-2022 走看看