<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>

                [TOC] ## 測試工具 推薦測試工具:http://coolaf.com/tool/chattest 項目啟動后的websocket地址: ![](https://img.kancloud.cn/a5/72/a572b9280d2a952c325429e0bceb4924_895x552.png) 一直發送消息就不會斷開鏈接,Netty讀寫空閑的時候,可以通過設置主動斷開連接。 ## 對接流程 ![](https://img.kancloud.cn/b9/95/b9955bdf72ff9b2956c9f889d99f00d0_1132x327.png) ### 消息類型 代碼中預設了以下幾種action: ```java public enum MsgActionEnum { CONNECT(1, "第一次(或重連)初始化連接"), CHAT(2, "聊天消息"), SIGNED(3, "消息簽收"), KEEPALIVE(4, "客戶端保持心跳"), PULL_FRIEND(5, "拉取好友"); public final Integer type; public final String content; MsgActionEnum(Integer type, String content){ this.type = type; this.content = content; } public Integer getType() { return type; } } ``` ## 發送測試 ### 建立連接 ```json { "action": "1", "chatMsg": { "senderId": "10000", "msg": "這是一條登錄消息", "receiverId": null }, "extand": null } ``` 測試結果: ``` 客戶端被移除:channel id 為:c999a1cc 005056fffec00008-00005328-0000000b-38c05956b8a7fe83-7e0ea4d9 UserId:10000,ChannelId:005056fffec00008-00005328-0000000b-38c05956b8a7fe83-7e0ea4d9 進入寫空閑...... 進入讀空閑...... 進入寫空閑...... channel 關閉之前:users 的數量為:1 channel 關閉之后:users 的數量為:0 客戶端被移除:channel id 為:7e0ea4d9 ``` #### `action = 1`存在的意義是什么? ```java if(MsgActionEnum.CONNECT.type.equals(action)){ //2.1 當websocket 第一次open的時候,初始化channel,把用的channel 和 userid 關聯起來 String senderId = dataContent.getChatMsg().getSenderId(); UserChanelRel.put(senderId,channel); //測試 for (Channel c: users) { log.info(c.id().asLongText()); } UserChanelRel.output(); } ``` 把消息的發送者放到一個map中,標記該用戶的login狀態。 ### 聊天消息 ```json { "action": "2", "chatMsg": { "senderId": "10000", "receiverId": 10086, "msg": "天若有情天亦老" }, "extand": null } ``` ### 簽收消息 ```json { "action": "3", "chatMsg": { "senderId": "10000", "receiverId": null, "msg": null }, "extand": 23549256,224244 } ``` 通過接口獲取到的消息清單,標記為簽收狀態,消息編號存放到`extand`屬性中。 ### 心跳檢測 ```json { "action": "4", "chatMsg": { "senderId": "10000", "receiverId": null, "msg": "心跳保持" }, "extand": null } ``` 需要與服務端保持連接,否則讀寫均空閑的時候,服務端就會主動斷開鏈接。 ``` /** * 用于檢測channel 的心跳handler * 繼承ChannelInboundHandlerAdapter,目的是不需要實現ChannelRead0 這個方法 */ public class HeartBeatHandler extends ChannelInboundHandlerAdapter { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if(evt instanceof IdleStateEvent){ IdleStateEvent event = (IdleStateEvent)evt;//強制類型轉化 if(event.state()== IdleState.READER_IDLE){ System.out.println("進入讀空閑......"); }else if(event.state() == IdleState.WRITER_IDLE) { System.out.println("進入寫空閑......"); }else if(event.state()== IdleState.ALL_IDLE){ System.out.println("channel 關閉之前:users 的數量為:"+ChatHandler.users.size()); Channel channel = ctx.channel(); //資源釋放 channel.close(); System.out.println("channel 關閉之后:users 的數量為:"+ChatHandler.users.size()); } } } } ```
                  <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>

                              哎呀哎呀视频在线观看