日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

websokct 使用.NETty 實現(xiàn)

websocket 是一個全雙工的長連接通訊

話不多說直接上代碼。。

服務端的代碼實現(xiàn)

public void init() throws InterruptedException {
        EventLoopGroup boosGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(boosGroup, workGroup).channel(NIOServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline pipeline = ch.pipeline();
                            // http 編碼和解編碼 handler
                            pipeline.addLast(new HttpServerCodec());
                            // chunked 以塊的形式 handler
                            pipeline.addLast(new ChunkedWriteHandler());
                            // http 數(shù)據(jù)傳輸過程中是分段的, httpObjectAggregator 就可以給多個段做聚合操作,這就是瀏覽器發(fā)送大量數(shù)據(jù)時候,就會發(fā)出多次 http 請求
                            pipeline.addLast(new HttpObjectAggregator(8192));
                            // webSocket 是通過幀的形式傳遞的,可以看到 WebSocketFrame 六個子類,WebSocketServerProtocolHandler 他的主要功能就是可以把 http 協(xié)議升級為 ws 協(xié)議,保持長連接
                            pipeline.addLast(new WebSocketServerProtocolHandler("/msg"));
                            pipeline.addLast(new CustomWebSocketFrameTextHandler());
                        }
                    });
            ChannelFuture channelFuture = serverBootstrap.bind(88).sync();
            channelFuture.addListener(future -> {
                if (future.isDone()) log.info("端口綁定成功");
            });
            channelFuture.channel().closeFuture().sync();
        } finally {
            boosGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

服務端的邏輯會用到自定義handler的處理器
CustomWebSocketFrameTextHandler 這個自定義處理器。

處理器的代碼

/**
     * 這里的 {@link TextWebSocketFrame} 表示一個文本幀
     *
     * @author L
     */
    class CustomWebSocketFrameTextHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

        Logger log = LoggerUtils.getLogger(CustomWebSocketFrameTextHandler.class);

        /**
         * 當 web 客戶端連接以后就會觸發(fā)這個方法
         */
        @Override
        public void handlerAdded(ChannelHandlerContext ctx) {
            // id 表示唯一的值信息,LongText 是唯一的。ShortText 不是唯一的
            log.info("handlerAdded => {} 被調(diào)用", ctx.channel().id().asLongText());
            log.info("handlerAdded => {} 被調(diào)用", ctx.channel().id().asShortText());
        }

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
            String text = msg.text();
            log.info("{}", text);
            // 回復客戶端消息
            ctx.writeAndFlush(new TextWebSocketFrame(text));
        }

        /**
         * 斷開連接以后會觸發(fā)
         */
        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) {
            log.info("handlerRemoved => {} 被調(diào)用", ctx.channel().id().asLongText());
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            log.error("發(fā)生異常 => {}", cause.getMessage());
        }
    }

服務端這就可以運行了。然后需要一個客戶端來測試??蛻舳诵枰苯泳帉懸粋€ html 的簡單的小頁面來測試。

let webSocket = null;

    if (window.WebSocket) {
        // 邏輯
        webSocket = new WebSocket("ws://localhost:88/msg");
        webSocket.onopen = () => {
            let showPlace = document.getElementById("responseMessage");
            showPlace.value = "連接開啟";
        }
        webSocket.onmessage = function (p) {
            let showPlace = document.getElementById("responseMessage");
            showPlace.value = showPlace.value + "rn" + p.data;
        }
        webSocket.onclose = () => {
            let showPlace = document.getElementById("responseMessage");
            showPlace.value = showPlace.value + "rn" + "連接關閉";
        }
        webSocket.onerror = (p) => {
            let showPlace = document.getElementById("responseMessage");
            showPlace.value = showPlace.value + "rn" + "連接錯誤 => " + p.type;
        }
    } else alert("當前瀏覽器不支持操作");

    function send(msg) {
        if (webSocket.OPEN) {
            webSocket.send(msg);
        }
    }

這樣就實現(xiàn)了 netty的 websocket 服務

分享到:
標簽:webSocket
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定