1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package io.netty.example.http.websocketx.client;
39
40 import io.netty.channel.Channel;
41 import io.netty.channel.ChannelFuture;
42 import io.netty.channel.ChannelHandlerContext;
43 import io.netty.channel.ChannelPromise;
44 import io.netty.channel.SimpleChannelInboundHandler;
45 import io.netty.handler.codec.http.FullHttpResponse;
46 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
47 import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
48 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
49 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
50 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
51 import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException;
52 import io.netty.util.CharsetUtil;
53
54 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
55
56 private final WebSocketClientHandshaker handshaker;
57 private ChannelPromise handshakeFuture;
58
59 public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
60 this.handshaker = handshaker;
61 }
62
63 public ChannelFuture handshakeFuture() {
64 return handshakeFuture;
65 }
66
67 @Override
68 public void handlerAdded(ChannelHandlerContext ctx) {
69 handshakeFuture = ctx.newPromise();
70 }
71
72 @Override
73 public void channelActive(ChannelHandlerContext ctx) {
74 handshaker.handshake(ctx.channel());
75 }
76
77 @Override
78 public void channelInactive(ChannelHandlerContext ctx) {
79 System.out.println("WebSocket Client disconnected!");
80 }
81
82 @Override
83 public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
84 Channel ch = ctx.channel();
85 if (!handshaker.isHandshakeComplete()) {
86 try {
87 handshaker.finishHandshake(ch, (FullHttpResponse) msg);
88 System.out.println("WebSocket Client connected!");
89 handshakeFuture.setSuccess();
90 } catch (WebSocketHandshakeException e) {
91 System.out.println("WebSocket Client failed to connect");
92 handshakeFuture.setFailure(e);
93 }
94 return;
95 }
96
97 if (msg instanceof FullHttpResponse) {
98 FullHttpResponse response = (FullHttpResponse) msg;
99 throw new IllegalStateException(
100 "Unexpected FullHttpResponse (getStatus=" + response.status() +
101 ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
102 }
103
104 WebSocketFrame frame = (WebSocketFrame) msg;
105 if (frame instanceof TextWebSocketFrame) {
106 TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
107 System.out.println("WebSocket Client received message: " + textFrame.text());
108 } else if (frame instanceof PongWebSocketFrame) {
109 System.out.println("WebSocket Client received pong");
110 } else if (frame instanceof CloseWebSocketFrame) {
111 System.out.println("WebSocket Client received closing");
112 ch.close();
113 }
114 }
115
116 @Override
117 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
118 cause.printStackTrace();
119 if (!handshakeFuture.isDone()) {
120 handshakeFuture.setFailure(cause);
121 }
122 ctx.close();
123 }
124 }