`
san_yun
  • 浏览: 2602467 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

netty初探

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics