FullHttpRequest和WebSocketFrame

FullHttpRequest 和 WebSocketFrame 是用于不同协议下处理网络请求的类。FullHttpRequest 是 Netty 框架中的一个类,用于处理传统的 HTTP 请求。而 WebSocketFrame 则是用于处理 WebSocket 数据帧的各种类型。在实际应用中,通常需要转换 HTTP 请求到 WebSocket 握手,以及处理后续的 WebSocket 帧。

FullHttpRequest 和 WebSocketFrame 概述

FullHttpRequest:

FullHttpRequest 是 Netty 中用于表示完整的 HTTP 请求的接口,继承了 HttpRequest 接口以及 FullHttpMessage。
它包含 HTTP 请求的所有部分,包括头部(Headers)、查询参数(Query Params)、主体(Body)等。
常用在 HTTP 服务端处理器中,用于处理客户端发来的请求。

WebSocketFrame:

WebSocketFrame 是 Netty 中用于表示 WebSocket 数据帧的抽象类。
不同类型的 WebSocket 数据帧继承自 WebSocketFrame,包括但不限于 TextWebSocketFrame(文本帧)、BinaryWebSocketFrame(二进制帧)、PingWebSocketFrame、PongWebSocketFrame、CloseWebSocketFrame 等。
在 WebSocket 服务端处理器中,接收到的数据会以 WebSocketFrame 的子类形式出现。
生成 WebSocket 握手响应的示例代码
首先,我们需要处理 HTTP 握手请求,将其转换为 WebSocket 连接。这通常在 HTTP 服务器中进行,将 HTTP 请求转换为 WebSocket 握手响应.

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.CharsetUtil;

public class WebSocketServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    private WebSocketServerHandshaker handshaker;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        if (msg instanceof FullHttpRequest) {
            handleHttpRequest(ctx, (FullHttpRequest) msg);
        } else if (msg instanceof WebSocketFrame) {
            handleWebSocketFrame(ctx, (WebSocketFrame) msg);
        }
    }

    private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
        // 检查请求的解码结果
        if (!req.decoderResult().isSuccess() || !"websocket".equals(req.headers().get(HttpHeaderNames.UPGRADE))) {
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
            return;
        }

        // 建立 WebSocket 握手
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                getWebSocketLocation(req), null, false);
        handshaker = wsFactory.newHandshaker(req);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), req);
        }
    }

    private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
        // 判断是否是关闭链路的指令
        if (frame instanceof CloseWebSocketFrame) {
            handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
            return;
        }

        // 判断是否是Ping消息
        if (frame instanceof PingWebSocketFrame) {
            ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
            return;
        }

        // 仅支持文本消息,不支持二进制消息
        if (!(frame instanceof TextWebSocketFrame)) {
            throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName()));
        }

        // 返回应答消息
        String request = ((TextWebSocketFrame) frame).text();
        ctx.channel().write(new TextWebSocketFrame(request + ", 欢迎使用Netty WebSocket服务,现在时刻:" + new java.util.Date().toString()));
    }
    
    private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
        // 返回应答给客户端
        if (res.status().code() != 200) {
            ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
            res.content().writeBytes(buf);
            buf.release();
            HttpHeaders.setContentLength(res, res.content().readableBytes());
        }

        // 如果是非Keep-Alive,关闭连接
        ctx.channel().writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
    }

    private String getWebSocketLocation(FullHttpRequest req) {
        String location = req.headers().get(HttpHeaderNames.HOST) + "/websocket";
        if (WebSocketServer.SSL) {
            return "wss://" + location;
        } else {
            return "ws://" + location;
        }
    }
}

代码解释
处理 HTTP 请求:

检查是否为 WebSocket 升级请求。如果不是,则返回一个 HTTP 400 错误响应。
使用 WebSocketServerHandshakerFactory 创建 WebSocket 握手处理器,并处理握手。
处理 WebSocket 帧:

处理关闭帧(CloseWebSocketFrame):关闭连接。
处理 Ping 帧(PingWebSocketFrame):响应 Pong 帧。
处理文本帧(TextWebSocketFrame):处理并响应文本消息。此示例只支持处理文本消息,实际应用中可能需要处理二进制消息等。
辅助方法:

sendHttpResponse:发送 HTTP 响应,必要时关闭连接。
getWebSocketLocation:获取 WebSocket 的 URL,处理 SSL 和非 SSL 情况。
总结
通过上述代码示例,你可以看到如何从 HTTP 请求处理 WebSocket 升级,并处理后续的 WebSocket 帧。FullHttpRequest 和 WebSocketFrame 分别对应 HTTP 协议和 WebSocket 协议的数据处理,两者在 WebSocket 握手时结合使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值