查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.example.stomp.websocket;
17  
18  import io.netty.channel.ChannelHandler.Sharable;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.MessageToMessageCodec;
21  import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
22  import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
23  import io.netty.handler.codec.http.websocketx.WebSocketFrame;
24  import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
25  import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
26  import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.HandshakeComplete;
27  import io.netty.handler.codec.stomp.StompSubframe;
28  import io.netty.handler.codec.stomp.StompSubframeAggregator;
29  import io.netty.handler.codec.stomp.StompSubframeDecoder;
30  
31  import java.util.List;
32  
33  @Sharable
34  public class StompWebSocketProtocolCodec extends MessageToMessageCodec<WebSocketFrame, StompSubframe> {
35  
36      private final StompChatHandler stompChatHandler = new StompChatHandler();
37      private final StompWebSocketFrameEncoder stompWebSocketFrameEncoder = new StompWebSocketFrameEncoder();
38  
39      @Override
40      public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
41          if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) {
42              StompVersion stompVersion = StompVersion.findBySubProtocol(((HandshakeComplete) evt).selectedSubprotocol());
43              ctx.channel().attr(StompVersion.CHANNEL_ATTRIBUTE_KEY).set(stompVersion);
44              ctx.pipeline()
45                 .addLast(new WebSocketFrameAggregator(65536))
46                 .addLast(new StompSubframeDecoder())
47                 .addLast(new StompSubframeAggregator(65536))
48                 .addLast(stompChatHandler)
49                 .remove(StompWebSocketClientPageHandler.INSTANCE);
50          } else {
51              super.userEventTriggered(ctx, evt);
52          }
53      }
54  
55      @Override
56      protected void encode(ChannelHandlerContext ctx, StompSubframe stompFrame, List<Object> out) throws Exception {
57          stompWebSocketFrameEncoder.encode(ctx, stompFrame, out);
58      }
59  
60      @Override
61      protected void decode(ChannelHandlerContext ctx, WebSocketFrame webSocketFrame, List<Object> out) {
62          if (webSocketFrame instanceof TextWebSocketFrame || webSocketFrame instanceof BinaryWebSocketFrame) {
63              out.add(webSocketFrame.content().retain());
64          } else {
65              ctx.close();
66          }
67      }
68  }