package com.zksy.pole.MQTTServer.channel;

/**
 * 通道初始化
 * @author ShaoYang
 * @date 2024/03/13 15:51
 */


import com.zksy.pole.MQTTServer.handler.MQTTMessageHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor
public class MqttChannelInit extends ChannelInitializer<SocketChannel> {
    @Autowired
    private MQTTMessageHandler mqttMessageHandler;

    @Override
    protected void initChannel(SocketChannel channel) {
        channel.pipeline()
                // 心跳时间
                .addLast("idle", new IdleStateHandler(600, 600, 1200, TimeUnit.SECONDS))
                .addLast("encoder", MqttEncoder.INSTANCE)
                .addLast("decoder", new MqttDecoder())
                .addLast(mqttMessageHandler);
    }

}

