zoukankan      html  css  js  c++  java
  • Netty游戏服务器二

    上节我们写个server主类,那么发现什么事情都干不了,是的,我们还没有做任何的业务处理。

    接着我们开始写处理客户端连接,发送接收数据的类ServerHandler。

    public class ServerHandler extends ChannelHandlerAdapter{
    	@Override
    	public void channelActive(ChannelHandlerContext ctx) throws Exception  //当客户端连上服务器的时候会触发此函数
    	{
    		System.out.println("clinet:" + ctx.channel().id() + " join server");
    	}
    	@Override
    	public void channelInactive(ChannelHandlerContext ctx) throws Exception//当客户端断开连接的时候触发函数
    	{
    		System.out.println("clinet:" + ctx.channel().id() + " leave server");
    		//User.onlineUser.remove(LoginDispatch.getInstance().user);
    	}
    	@Override
    	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception//当客户端发送数据到服务器会触发此函数
    	{
    		/*SocketModel message = (SocketModel) msg;
    		switch (message.getType()) {
    		case TypeProtocol.TYPE_LOGIN:
    			LoginDispatch.getInstance().dispatch(ctx, message);
    			break;
    		case TypeProtocol.TYPE_WIZARD:
    			WizardDispatch.getInstance().dispatch(ctx, message);
    			break;
    		case TypeProtocol.TYPE_USER:
    			UserDispatch.getInstance().dispatch(ctx, message);
    			break;
    		case TypeProtocol.TYPE_BATTLE:
    			BattleDispatch.getInstance().dispatch(ctx, message);
    		default:
    			break;
    		}
    		/*
                这里我先把代码注释掉,现在还没讲到 */
            */ } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //cause.printStackTrace(); } }

      写完服务器主要的业务处理类,接着我们要把这个类的对象添加到服务器Channel的pipeline中。

    在之前我们写的GameServer中,添加

    ch.pipeline().addLast(new ServerHandler());
    ServerBootstrap b = new ServerBootstrap();//server启动管理配置
    			b.group(bossGroup, workGroup)
    			.channel(NioServerSocketChannel.class)
    			.option(ChannelOption.SO_BACKLOG, 1024)//最大客户端连接数为1024
    			.childHandler(new ChannelInitializer<SocketChannel>() {
    				@Override
    				protected void initChannel(SocketChannel ch) throws Exception {
    					ch.pipeline().addLast(new ServerHandler());
    				}
    			});
    

      

  • 相关阅读:
    UISegmentControl
    UISwitch 开关控件
    UI弹出键盘和收回键盘
    UITextField的属性设置
    UIButton的属性设置
    UILabel 的属性设置
    创建UIView对象
    id和instancetype的异同
    SPOJ FIBPOL
    HDU 6168 Numbers
  • 原文地址:https://www.cnblogs.com/CaomaoUnity3d/p/4609171.html
Copyright © 2011-2022 走看看