netty如何处理多种类型请求?

爱喝貓的咖啡 发布于 2014/10/27 16:15
阅读 999
收藏 0

如下代码,请求有多种类型:字符串、http、websocket、socket等,如何用合适的handler处理指定的请问:

比如 SocketHandler处理Socket请求,HttpHandler处理Http请求。


public class DemoChannelPipelineFactory implements ChannelPipelineFactory{

@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();

// pipeline.addLast("hqd", new HttpRequestDecoder());
// pipeline.addLast("hqc", new HttpRequestEncoder());

// pipeline.addLast("httpResponseDecoder", new HttpResponseDecoder());
// pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder());

// pipeline.addLast("webSocketFrameDecoder", new WebSocket00FrameDecoder());
// pipeline.addLast("webSocketFrameEncoder", new WebSocket00FrameEncoder());

// pipeline.addLast("sd", new StringDecoder());
// pipeline.addLast("se", new StringEncoder());

pipeline.addLast("wsServerHandler", new WebsocketServerHandler());

return pipeline;
}
}



public class SocketHandler extends SimpleChannelUpstreamHandler {
@Override
     public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception{
Object message = e.getMessage();
 
System.out.println("message:"+message.getClass());
 
// if(message instanceof HttpRequest){
//  
// }else if(message instanceof WebSocketFrame){
//  
// }else{  
// }

super.messageReceived(ctx, e);
}

加载中
0
轨迹david
轨迹david
不建议这么做,建议分端口来, 字符串、http、websocket、socket 这些请求不容易区分开,比如字符串请求完全可以仿http,这样就无法区分究竟是字符传还是http
返回顶部
顶部