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

com.iteaj.iot.client.codec.WebSocketClient Maven / Gradle / Ivy

The newest version!
package com.iteaj.iot.client.codec;

import com.iteaj.iot.CoreConst;
import com.iteaj.iot.client.TcpSocketClient;
import com.iteaj.iot.client.websocket.WebSocketClientComponentAbstract;
import com.iteaj.iot.client.websocket.WebSocketClientConnectProperties;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.handler.codec.http.HttpClientCodec;

import java.util.List;

import static com.iteaj.iot.CoreConst.CLIENT_ENCODER_HANDLER;

public class WebSocketClient extends TcpSocketClient {

    public WebSocketClient(WebSocketClientComponentAbstract clientComponent, WebSocketClientConnectProperties config) {
        super(clientComponent, config);
    }

    @Override
    protected ChannelInboundHandler createProtocolDecoder() {
        return new WebSocketClientDecoder(this);
    }

    @Override
    protected void doInitChannel(Channel channel) {
        super.doInitChannel(channel);
        // 替换掉编码器
        channel.pipeline().replace(CLIENT_ENCODER_HANDLER, CLIENT_ENCODER_HANDLER, getClientComponent().getWebSocketEncoder());
        channel.pipeline().addFirst(new HttpClientCodec());
    }

    @Override
    public WebSocketClientConnectProperties getConfig() {
        return (WebSocketClientConnectProperties) super.getConfig();
    }

    @Override
    public WebSocketClientComponentAbstract getClientComponent() {
        return (WebSocketClientComponentAbstract) super.getClientComponent();
    }

    @Override
    public synchronized void reconnection() {
        Object o = getChannel().attr(CoreConst.WEBSOCKET_CLOSE).get();
        /**
         * 属于关闭请求, 直接移除客户端
         * @see com.iteaj.iot.websocket.WebSocketEncoder#encode(ChannelHandlerContext, Object, List)
         */
        if(o instanceof Boolean) {
            this.close();
        } else {
            super.reconnection();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy