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

com.feingto.iot.server.handler.mqtt.DisconnectHandler Maven / Gradle / Ivy

There is a newer version: 1.2.5.RELEASE
Show newest version
package com.feingto.iot.server.handler.mqtt;

import com.feingto.iot.common.Constants;
import com.feingto.iot.server.cache.SessionCache;
import com.feingto.iot.server.handler.BaseMessageHandler;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessageType;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 中断连接处理器
 *
 * @author longfei
 */
@Slf4j
public class DisconnectHandler extends BaseMessageHandler {
    public DisconnectHandler() {
        super(MqttMessageType.DISCONNECT);
    }

    @Override
    public void handle(Channel channel, Object object) {
        String clientId = channel.attr(Constants.KEY_CLIENT_ID).get();
        Optional.ofNullable(SessionCache.getInstance().get(clientId))
                .ifPresent(sess -> {
                    // 从存储的会话状态中移除遗嘱消息
                    if (sess.willMessage() != null) {
                        log.debug(">>> remove will message of the {}", clientId);
                        sess.willMessage(null);
                    }
                    // cleanSession为true时清除会话
                    if (sess.cleanSession()) {
                        log.debug(">>> clean session of the {}", clientId);
                        this.cleanSession(clientId);
                    }
                    SessionCache.getInstance().remove(clientId);
                });
        channel.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy