1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.stomp.websocket;
17
18 import io.netty.channel.ChannelInitializer;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.handler.codec.http.HttpObjectAggregator;
21 import io.netty.handler.codec.http.HttpServerCodec;
22 import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
23 import io.netty.util.internal.ObjectUtil;
24
25 public class StompWebSocketChatServerInitializer extends ChannelInitializer<SocketChannel> {
26
27 private final String chatPath;
28 private final StompWebSocketProtocolCodec stompWebSocketProtocolCodec;
29
30 public StompWebSocketChatServerInitializer(String chatPath) {
31 this.chatPath = ObjectUtil.checkNotNull(chatPath, "chatPath");
32 stompWebSocketProtocolCodec = new StompWebSocketProtocolCodec();
33 }
34
35 @Override
36 protected void initChannel(SocketChannel channel) throws Exception {
37 channel.pipeline()
38 .addLast(new HttpServerCodec())
39 .addLast(new HttpObjectAggregator(65536))
40 .addLast(StompWebSocketClientPageHandler.INSTANCE)
41 .addLast(new WebSocketServerProtocolHandler(chatPath, StompVersion.SUB_PROTOCOLS))
42 .addLast(stompWebSocketProtocolCodec);
43 }
44 }