<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、概述 可以集成netty,作為通訊框架,將應用作為一個駐留監聽通訊程序; >[danger] 平臺集成netty的時候,為避免傳統的方式集成導致阻塞主線程,需要用線程池異步的方式來啟動Netty方可; ## 二、配置 ``` rayframework.enabled.socket=true ``` ``` rayframework.socket.netty-daemon-port=6666 ``` ### **ChannelInboundHandlerAdapter配置** 無需配置,按照約定實現指定接口即可,如果系統中,對該接口提供了多于一個實現,則需要指定啟用的那個實現,用@RayConventionEnabled注解它即可; ## 三、定制 主要的工作,就是實現自己的ChannelInboundHandlerAdapter 類; 參考: ``` public class RayNettyChannelInboundHandler extends ChannelInboundHandlerAdapter { private Logger logger = LoggerFactory.getLogger(RayNettyChannelInboundHandler.class); private SysLogDao sysLogDao = ServerIocHolder.getBean(SysLogDao.class); /** * 從客戶端收到新的數據時,這個方法會在收到消息時被調用 * @param ctx * @param msg */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception, IOException { ByteBuf byteBuf = (ByteBuf) msg; SysLog log = new SysLog(); log.setOperateContent(byteBuf.toString(CharsetUtil.UTF_8)); sysLogDao.save(log); ctx.writeAndFlush(Unpooled.copiedBuffer("你好啊,客戶端", Charset.forName("GBK"))); } /** * 從客戶端收到新的數據、讀取完成時調用 * @param ctx */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws IOException { logger.info("channelReadComplete"); ctx.flush(); } /** * 當出現 Throwable 對象才會被調用,即當 Netty 由于 IO 錯誤或者處理器在處理事件時拋出的異常時 * @param ctx * @param cause */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws IOException { logger.info("channelReadComplete"); cause.printStackTrace(); ctx.close(); } /** * 客戶端與服務端第一次建立連接時 執行 * @param ctx * @throws Exception */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception, IOException { super.channelActive(ctx); ctx.channel().read(); InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); String clientIp = insocket.getAddress().getHostAddress(); logger.info("channelActive:{},{}", clientIp, ctx.name()); } /** * 客戶端與服務端斷連時執行 * @param ctx * @throws Exception */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception, IOException { super.channelInactive(ctx); InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); String clientIp = insocket.getAddress().getHostAddress(); ctx.close(); logger.info("channelInactive{}" + clientIp); } /** * 服務端當read超時, 會調用這個方法 * @param ctx * @param evt * @throws Exception */ @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception, IOException { super.userEventTriggered(ctx, evt); InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); String clientIp = insocket.getAddress().getHostAddress(); ctx.close(); logger.info("userEventTriggered{}", clientIp); } @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { logger.info("channelRegistered"); } @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { logger.info("channelUnregistered"); } @Override public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { logger.info("channelWritabilityChanged"); } } ``` >[danger] 特別注意: > 1、業務處理器類中,無法直接用@Autowired獲取注入的資源,必須用ServerIocHolder.getBean()的方式獲取;
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看