阅读目录

<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>5.0.0.Alpha2</version></dependency>
2、处理 HTTP 协议的 ChannelHandler —— 非 WebSocket 协议的请求,返回 index.html 页面
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> { private final String wsUri; private static File INDEX; static { URL location = HttpRequestHandler.class.getProtectionDomain().getCodeSource().getLocation(); try { String path = location.toURI() + "index.html"; path = !path.contains("file:") ? path : path.substring(5); INDEX = new File(path); } catch (URISyntaxException e) { e.printStackTrace(); } } public HttpRequestHandler(String wsUri) { this.wsUri = wsUri; } @Override protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { // 如果请求了Websocket,协议升级,增加引用计数(调用retain()),并将他传递给下一个 ChannelHandler // 之所以需要调用 retain() 方法,是因为调用 channelRead() 之后,资源会被 release() 方法释放掉,需要调用 retain() 保留资源 if (wsUri.equalsIgnoreCase(request.uri())) { ctx.fireChannelRead(request.retain()); } else { //处理 100 Continue 请求以符合 HTTP 1.1 规范 if (HttpHeaderUtil.is100ContinueExpected(request)) { send100Continue(ctx); } // 读取 index.html RandomAccessFile randomAccessFile = new RandomAccessFile(INDEX, "r"); HttpResponse response = new DefaultHttpResponse(request.protocolVersion(), HttpResponseStatus.OK); HttpHead
关键字:
青岛软件培训
可能你正在寻找一家靠谱的IT培训机构,
渴望突破职业瓶颈,
找一份得体的工作。
恰巧万码学堂正在寻找像你这样不甘平凡的追光者!
我们拒绝纸上谈兵,直接参与真实开发流程!
现在行动,未来可期
立即拨打0532-85025005,预约免费职业规划咨询
前20名咨询者赠送《2025高薪技术岗位白皮书》!
你不是在报名课程,而是在投资五年后的自己!

