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){
        
            }
        }
    }
  • 相关阅读:
    CSS选择器实现搜索功能 驱动过滤搜索技术
    js实现倒计时 类似团购网站
    SQL Server系统表sysobjects介绍与使用
    四种开机的奇葩方法 设置定时开机
    sass 使用小记
    flex 弹性布局
    margin padding width height left top right bottom 百分比
    vue中canvas 实现手势密码
    babel-polyfill(解决浏览器不支持es6的问题)和es6-promise(解决不支持promise的问题)
    Ajax fetch axios的区别与优缺点
  • 原文地址:https://www.cnblogs.com/self-crossing/p/11011703.html
Copyright © 2011-2022 走看看