Example #1
0
 @Override
 protected void doMessageReceived(ClientConnection conn, ChannelHandlerContext ctx, Object msg) {
   if (conn == null || conn.isClosed()) {
     return;
   }
   boolean valid = false;
   if (msg instanceof HttpResponse) {
     HttpResponse response = (HttpResponse) msg;
     conn.handleResponse(response);
     valid = true;
   }
   if (msg instanceof HttpContent) {
     HttpContent chunk = (HttpContent) msg;
     if (chunk.content().isReadable()) {
       Buffer buff = new Buffer(chunk.content().slice());
       conn.handleResponseChunk(buff);
     }
     if (chunk instanceof LastHttpContent) {
       conn.handleResponseEnd((LastHttpContent) chunk);
     }
     valid = true;
   } else if (msg instanceof WebSocketFrame) {
     WebSocketFrame frame = (WebSocketFrame) msg;
     switch (frame.getType()) {
       case BINARY:
       case TEXT:
         conn.handleWsFrame(frame);
         break;
       case PING:
         // Echo back the content of the PING frame as PONG frame as specified in RFC 6455
         // Section 5.5.2
         ctx.writeAndFlush(
             new DefaultWebSocketFrame(WebSocketFrame.FrameType.PONG, frame.getBinaryData()));
         break;
       case CLOSE:
         if (!closeFrameSent) {
           // Echo back close frame and close the connection once it was written.
           // This is specified in the WebSockets RFC 6455 Section  5.4.1
           ctx.writeAndFlush(frame).addListener(ChannelFutureListener.CLOSE);
           closeFrameSent = true;
         }
         break;
     }
     valid = true;
   }
   if (!valid) {
     throw new IllegalStateException("Invalid object " + msg);
   }
 }
Example #2
0
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

      Channel ch = e.getChannel();
      ClientConnection conn = connectionMap.get(ch);
      Object msg = e.getMessage();
      if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        conn.handleResponse(response);
        ChannelBuffer content = response.getContent();
        if (content.readable()) {
          conn.handleResponseChunk(new Buffer(content));
        }
        if (!response.isChunked()) {
          conn.handleResponseEnd();
        }
      } else if (msg instanceof HttpChunk) {
        HttpChunk chunk = (HttpChunk) msg;
        if (chunk.getContent().readable()) {
          Buffer buff = new Buffer(chunk.getContent());
          conn.handleResponseChunk(buff);
        }
        if (chunk.isLast()) {
          if (chunk instanceof HttpChunkTrailer) {
            HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
            conn.handleResponseEnd(trailer);
          } else {
            conn.handleResponseEnd();
          }
        }
      } else if (msg instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) msg;
        conn.handleWsFrame(frame);
      } else {
        throw new IllegalStateException("Invalid object " + e.getMessage());
      }
    }