netty初探

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 15:00   11   0

netty API

//启动类

 ServerBootstrap bootstrap = new
  ServerBootstrap( NioServerSocketChannelFactory(new
 Executors.newCachedThreadPool()),Executors.newCachedThreadPool());

//关键的部分,Set up the event pipeline factory.

bootstrap.setPipelineFactory(new
 WebSocketServerPipelineFactory());

// Bind and start to accept incoming connections.

 bootstrap.bind(new
 InetSocketAddress(8080));

WebSocketServerPipelineFactory部分:

public
 class WebSocketServerpipelineFactory implements
 ChannelPipelineFactory{

 public
 ChannelPipeline getPipeline(){
                  //创建一个默认的pipeline implemenation.

                  ChannelPipeline pipeline = pipeline();
                  pipeline.addLast("decoder"
,new
 HttpRequestDecoder());
                  pipeline.addLast("aggregator"
,new
 HttpChunkAggregator(65536));
    pipeline.addLast("encoder"
,new
 HttpResponseEncoder());
                  pipeline.addLast("handler"
,new
 WebSocketServerhandler());

       }
}

WebSocketServerhandler部分:

public
 class WebSocketServerHandler extends
 SimpleChannelUpstreamHandler {
 
  private
 static
 final
 String
 WEBSOCKET_PATH = "/websocket"
;

 public
 void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) {  
    Object
 message = evt.getMessage();  
    // Do something with the received message.  

    ...  
    // And forward the event to the next handler.  

    ctx.sendUpstream(evt);  
 }
}

netty 文档

http://www.jboss.org/netty/documentation.html
http://jef.javaeye.com/blog/544206

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP