处理HTTP请求HttpRequestHandler.javapublicclassHttpRequestHandlerextendsSimpleChannelInboundHandler{//1??privatefinalStringwsUri;??privatestaticfinalFileINDEX;???static{????URLlocation=HttpRequestHandler.class.getProtectionDomain().getCodeSource().getLocation();????try{??????Stringpath=location.toURI()+"WebsocketChatClient.html";??????path=!path.contains("file:")?path:path.substring(5);??????INDEX=newFile(path);????}catch(URISyntaxExceptione){??????thrownewIllegalStateException("UnabletolocateWebsocketChatClient.html",e);????}??}???publicHttpRequestHandler(StringwsUri){????this.wsUri=wsUri;??}???@Override??publicvoidchannelRead0(ChannelHandlerContextctx,FullHttpRequestrequest)throwsException{????if(wsUri.equalsIgnoreCase(request.getUri())){??????ctx.fireChannelRead(request.retain());?????????//2????}else{??????if(HttpHeaders.is100ContinueExpected(request)){????????send100Continue(ctx);???????????????//3??????}???????RandomAccessFilefile=newRandomAccessFile(INDEX,"r");//4???????HttpResponseresponse=newDefaultHttpResponse(request.getProtocolVersion(),HttpResponseStatus.OK);??????response.essay-headers().set(HttpHeaders.Names.CONTENT_TYPE,"text/html;charset=UTF-8");???????booleankeepAlive=HttpHeaders.isKeepAlive(request);???????if(keepAlive){????????????????????//5????????response.essay-headers().set(HttpHeaders.Names.CONTENT_LENGTH,file.length());????????response.essay-headers().set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.KEEP_ALIVE);??????}??????ctx.write(response);??????????//6???????if(ctx.pipeline().get(SslHandler.class)==null){??//7????????ctx.write(newDefaultFileRegion(file.getChannel(),0,file.length()));??????}else{????????ctx.write(newChunkedNioFile(file.getChannel()));??????}??????ChannelFuturefuture=ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);?????//8??????if(!keepAlive){????????future.addListener(ChannelFutureListener.CLOSE);????//9??????}???????file.close();????}??}???privatestaticvoidsend100Continue(ChannelHandlerContextctx){????FullHttpResponseresponse=newDefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.CONTINUE);????ctx.writeAndFlush(response);??}???@Override??publicvoidexceptionCaught(ChannelHandlerContextctx,Throwablecause)??????throwsException{????Channelincoming=ctx.channel();????System.out.println("Client:"+incoming.remoteAddress()+"异常");????//当出现异常就关闭连接????cause.printStackTrace();????ctx.close();??}}1.扩展SimpleChannelInboundHandler用于处理FullHttpRequest信息2.如果请求是WebSocket升级,递增引用计数器(保留)并且将它传递给在ChannelPipeline中的下个ChannelInboundHandler3.处理符合HTTP1.1的“100Continue”请求4.读取默认的WebsocketChatClient.html页面5.判断keepalive是否在请求头里面6.写HttpResponse到客户端7.写index.html到客户端,判断SslHandler是否在ChannelPipeline来决定是使用DefaultFileRegion还是ChunkedNioFile8.写并刷新LastHttpContent到客户端,标记响应完成9.如果keepalive没有要求,当写完成时,关闭ChannelHttpRequestHandler做了下面几件事,如果该HTTP请求被发送到URI“/ws”,调用FullHttpRequest上的retain(),并通过调用fireChannelRead(msg)转发到下一个ChannelInboundHandler。【2018 世界杯投注网址】 一旦升级连接将使用的WebSocket传输所有数据。据介绍,目前公司共有近20辆房车,此外还和省内江铃旅居车达成合作关系。5.你可以设置这里指定的Channel实现的配置参数。
ban

版权所有:俄罗斯vs沙特阿拉伯投注  技术支持:仕德伟科技 苏ICP备15043650号

百度地图

起点 终点

在线客服

  • 1
  • 2
  • 3
  • 4
  • 5

在线客服

  • 1
  • 2
  • 3
  • 4
  • 5
Baidu