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

com.feingto.iot.server.handler.mqtt.PubrecHandler 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.common.service.mqtt.MessageResponse;
import com.feingto.iot.server.cache.MessageCache;
import com.feingto.iot.server.handler.BaseMessageHandler;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
import io.netty.handler.codec.mqtt.MqttMessageType;

import java.util.Optional;

/**
 * Qos level = 2的协议流的第二个消息,确认已经收到
 * 无论是订阅者还是服务器,在消费PUBREC消息之后需要发送一个PUBREL消息给发送者(和PUBREC具有同样的消息ID)
 *
 * @author longfei
 */
public class PubrecHandler extends BaseMessageHandler {
    public PubrecHandler() {
        super(MqttMessageType.PUBREC);
    }

    @Override
    public void handle(Channel channel, Object object) {
        MqttMessage msg = (MqttMessage) object;
        MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) msg.variableHeader();
        int messageId = variableHeader.messageId();

        // 消息转入等待pubcomp阶段
        Optional.ofNullable(MessageCache.getInstance(igniteMessage)
                .get(channel.attr(Constants.KEY_CLIENT_ID).get(), messageId))
                .ifPresent(message -> message.type(MqttMessageType.PUBREL));

        // 返回pubrel消息
        MessageResponse.pubrel(channel, messageId);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy