All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fastchar.socket.client.websocket.FastWebSocketClientChannelHandler Maven / Gradle / Ivy

package com.fastchar.socket.client.websocket;

import com.fastchar.core.FastChar;
import com.fastchar.socket.core.FastWebSocket;
import com.fastchar.socket.interfaces.IFastWebSocketClientListener;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;


public class FastWebSocketClientChannelHandler extends SimpleChannelInboundHandler {

    private final WebSocketClientHandshaker handshaker;
    private ChannelPromise handshakeFuture;
    private final IFastWebSocketClientListener iFastWebSocketClientListener;
    private final String url;
    private FastWebSocket webSocket;


    public FastWebSocketClientChannelHandler(WebSocketClientHandshaker handshaker, IFastWebSocketClientListener iFastWebSocketClientListener, String url) {
        this.handshaker = handshaker;
        this.iFastWebSocketClientListener = iFastWebSocketClientListener;
        this.url = url;
    }

    public ChannelFuture handshakeFuture() {
        return handshakeFuture;
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) {
        handshakeFuture = ctx.newPromise();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        handshaker.handshake(ctx.channel());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        if (iFastWebSocketClientListener != null) {
            iFastWebSocketClientListener.onClose(webSocket, null, -1);
        }
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, Object msg) {
        Channel ch = ctx.channel();
        if (!handshaker.isHandshakeComplete()) {
            FullHttpResponse response = (FullHttpResponse) msg;
            handshaker.finishHandshake(ch, response);
            handshakeFuture.setSuccess();
            webSocket = new FastWebSocket(ctx.channel(), url);
            if (iFastWebSocketClientListener != null) {
                iFastWebSocketClientListener.onOpen(webSocket, response);
            }
            return;
        }

        if (msg instanceof FullHttpResponse) {
            return;
        }

        WebSocketFrame frame = (WebSocketFrame) msg;
        if (frame instanceof TextWebSocketFrame) {
            TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
            if (iFastWebSocketClientListener != null) {
                iFastWebSocketClientListener.onMessage(webSocket, textFrame.text());
            }

        } else if (frame instanceof PongWebSocketFrame) {
            FastChar.getLogger().debug("WebSocket Client received pong");
        } else if (frame instanceof CloseWebSocketFrame) {
            ch.close();
            if (iFastWebSocketClientListener != null) {
                iFastWebSocketClientListener.onClose(webSocket, null, 0);
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        if (!handshakeFuture.isDone()) {
            handshakeFuture.setFailure(cause);
        }
        ctx.close();
        if (iFastWebSocketClientListener != null) {
            iFastWebSocketClientListener.onClose(webSocket, cause, -1);
        }
    }
}