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

com.feingto.iot.common.service.mqtt.MessageResponse Maven / Gradle / Ivy

There is a newer version: 2.3.3.RELEASE
Show newest version
package com.feingto.iot.common.service.mqtt;

import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.*;

/**
 * 消息响应
 *
 * @author longfei
 */
public class MessageResponse {
    /**
     * 连接响应
     */
    public static void connack(Channel channel, MqttConnectReturnCode returnCode) {
        channel.writeAndFlush(new MqttConnAckMessage(
                new MqttFixedHeader(MqttMessageType.CONNACK, false,
                        MqttQoS.AT_MOST_ONCE, false, 0x02),
                new MqttConnAckVariableHeader(returnCode, true)));
    }

    /**
     * 订阅响应
     */
    public static void suback(Channel channel, Iterable grantedQoSLevels, int messageId) {
        channel.writeAndFlush(MqttMessageFactory.newMessage(
                new MqttFixedHeader(MqttMessageType.SUBACK, false,
                        MqttQoS.AT_MOST_ONCE, false, 0),
                MqttMessageIdVariableHeader.from(messageId),
                new MqttSubAckPayload(grantedQoSLevels)));
    }

    /**
     * 取消订阅响应
     */
    public static void unsuback(Channel channel, int messageId) {
        channel.writeAndFlush(MqttMessageFactory.newMessage(
                new MqttFixedHeader(MqttMessageType.UNSUBACK, false,
                        MqttQoS.AT_MOST_ONCE, false, 0),
                MqttMessageIdVariableHeader.from(messageId), null));
    }

    /**
     * PUBLISH消息响应(Qos level = 1 或者 Qos level = 2的协议流的第一个消息)
     */
    public static void puback(Channel channel, MqttMessageType type, MqttQoS qoS, int messageId) {
        channel.writeAndFlush(new MqttPubAckMessage(
                new MqttFixedHeader(type, false, qoS, false, 0x02),
                MqttMessageIdVariableHeader.from(messageId)));
    }

    /**
     * Qos level = 2的协议流的第二个消息响应,确认已经收到
     */
    public static void pubrel(Channel channel, int messageId) {
        channel.writeAndFlush(new MqttMessage(
                new MqttFixedHeader(MqttMessageType.PUBREL, false,
                        MqttQoS.AT_LEAST_ONCE, false, 0x02),
                MqttMessageIdVariableHeader.from(messageId)));
    }

    /**
     * Qos level = 2的协议流的第三个消息响应
     */
    public static void pubcomp(Channel channel, int messageId) {
        channel.writeAndFlush(new MqttMessage(
                new MqttFixedHeader(MqttMessageType.PUBCOMP, false,
                        MqttQoS.AT_MOST_ONCE, false, 0x02),
                MqttMessageIdVariableHeader.from(messageId)));
    }

    /**
     * PING响应
     */
    public static void pingresp(Channel channel) {
        channel.writeAndFlush(new MqttMessage(
                new MqttFixedHeader(MqttMessageType.PINGRESP, false,
                        MqttQoS.AT_MOST_ONCE, false, 0)));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy