由Netty實現的WebSocket(推薦使用)

本文參考自:《Netty權威指南》

 一、添加依賴

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.42.Final</version>
</dependency>

二、新建一個ServerBootstrap對象,並設置WebSocketServerHandler 

public class Server {

    public static void run(final int port) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            // 加入http的解碼器
                            ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                            // 加入ObjectAggregator解碼器,作用是他會把多個消息轉換爲單一的FullHttpRequest或者FullHttpResponse
                            ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
                            // 加入http的編碼器
                            ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                            // 加入chunked 主要作用是支持異步發送的碼流(大文件傳輸),但不專用過多的內存,防止java內存溢出
                            ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                            // 加入自定義處理文件服務器的業務邏輯handler
                            ch.pipeline().addLast("webSocketServerHandler", new WebSocketServerHandler());
                        }
                    });
            ChannelFuture future = b.bind(port).sync();
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

三、新增自定義Handler處理WebSocket連接(繼承自SimpleChannelInboundHandler)

public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object> {

    private WebSocketServerHandshakerFactory factory;

    private WebSocketServerHandshaker handshaker;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object o) throws Exception {
        if (null == factory) {
            factory = new WebSocketServerHandshakerFactory("ws://localhost:8081/websocket", null, false);
        }
        //傳統的Http接入
        if (o instanceof FullHttpRequest) {
            handleHttpRequest(ctx, (FullHttpRequest) o);
        } else if (o instanceof WebSocketFrame) {//WebSocket接入
            handleWebsocketFrame(ctx, (WebSocketFrame) o);
        }

    }

    /**
     * 傳統的Http接入
     *
     * @param ctx
     * @param req
     */
    private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
        //如果Http解碼失敗,返回Http異常
        if (!req.decoderResult().isSuccess() || (!"websocket".equals(req.headers().get("Upgrade")))) {
            sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
            return;
        }

        handshaker = factory.newHandshaker(req);

        if (null == handshaker) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), req);
        }
    }

    /**
     * WebSocket接入
     *
     * @param ctx
     * @param frame
     */
    private void handleWebsocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception{
        //判斷是否是關閉鏈路的指令
        if (frame instanceof CloseWebSocketFrame) {
            handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
            return;
        }
        //判斷是否是ping消息,是則返回pong消息
        if (frame instanceof PingWebSocketFrame) {
            ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
            return;
        }

        //判斷是否是文本消息
        if (frame instanceof TextWebSocketFrame) {
            String request = ((TextWebSocketFrame) frame).text();
            ctx.channel().write(new TextWebSocketFrame("歡迎使用Netty WebSocket服務:" + request));
            return;
        }

        //判斷是否是二進制數據(功能強大,拓展性強)
        if (frame instanceof BinaryWebSocketFrame) {
            ByteBuf byteBuf = frame.content();
            // OutputStream outputStream = new ByteArrayOutputStream();
            // byteBuf.readBytes(outputStream, byteBuf.capacity());
        }
    }

    private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
        if (200 != res.status().code()) {
            ByteBuf byteBuf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
            res.content().writeBytes(byteBuf);
            byteBuf.release();
            HttpUtil.setContentLength(res, res.content().readableBytes());
        }
        //如果是非Keep-Alive,關閉連接
        ChannelFuture channelFuture = ctx.channel().writeAndFlush(req);
        if (!HttpUtil.isKeepAlive(req) || 200 != res.status().code()) {
            channelFuture.addListener(ChannelFutureListener.CLOSE);
        }
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

 

發佈了86 篇原創文章 · 獲贊 144 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章