View Javadoc

1   /*
2    * Copyright 2012 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    *   http://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  //The MIT License
17  //
18  //Copyright (c) 2009 Carl Bystršm
19  //
20  //Permission is hereby granted, free of charge, to any person obtaining a copy
21  //of this software and associated documentation files (the "Software"), to deal
22  //in the Software without restriction, including without limitation the rights
23  //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24  //copies of the Software, and to permit persons to whom the Software is
25  //furnished to do so, subject to the following conditions:
26  //
27  //The above copyright notice and this permission notice shall be included in
28  //all copies or substantial portions of the Software.
29  //
30  //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31  //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32  //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33  //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34  //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35  //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36  //THE SOFTWARE.
37  
38  package org.jboss.netty.example.http.websocketx.client;
39  
40  import org.jboss.netty.channel.Channel;
41  import org.jboss.netty.channel.ChannelHandlerContext;
42  import org.jboss.netty.channel.ChannelStateEvent;
43  import org.jboss.netty.channel.ExceptionEvent;
44  import org.jboss.netty.channel.MessageEvent;
45  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
46  import org.jboss.netty.handler.codec.http.HttpResponse;
47  import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
48  import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
49  import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
50  import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
51  import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
52  import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
53  import org.jboss.netty.util.CharsetUtil;
54  
55  public class WebSocketClientHandler extends SimpleChannelUpstreamHandler {
56  
57      private final WebSocketClientHandshaker handshaker;
58  
59      public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
60          this.handshaker = handshaker;
61      }
62  
63      @Override
64      public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
65          System.err.println("WebSocket Client disconnected!");
66      }
67  
68      @Override
69      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
70          Channel ch = ctx.getChannel();
71          if (!handshaker.isHandshakeComplete()) {
72              handshaker.finishHandshake(ch, (HttpResponse) e.getMessage());
73              System.err.println("WebSocket Client connected!");
74              return;
75          }
76  
77          if (e.getMessage() instanceof HttpResponse) {
78              HttpResponse response = (HttpResponse) e.getMessage();
79              throw new IllegalStateException(
80                      "unexpected response (status=" + response.getStatus() +
81                              ", content=" + response.getContent().toString(CharsetUtil.UTF_8) + ')');
82          }
83  
84          WebSocketFrame frame = (WebSocketFrame) e.getMessage();
85          if (frame instanceof TextWebSocketFrame) {
86              TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
87              System.err.println("WebSocket Client received message: " + textFrame.getText());
88          } else if (frame instanceof PongWebSocketFrame) {
89              System.err.println("WebSocket Client received pong");
90          } else if (frame instanceof CloseWebSocketFrame) {
91              System.err.println("WebSocket Client received closing");
92              ch.close();
93          } else if (frame instanceof PingWebSocketFrame) {
94              System.err.println("WebSocket Client received ping, response with pong");
95              ch.write(new PongWebSocketFrame(frame.getBinaryData()));
96          }
97      }
98  
99      @Override
100     public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
101         e.getCause().printStackTrace();
102         e.getChannel().close();
103     }
104 }