eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.89k stars 2.37k forks source link

IMPORTANT) QoS 2 Receive is weired #2625

Open Jisean opened 2 years ago

Jisean commented 2 years ago

Hi. I'm Mqtt Newbie.

I'm Studing Mqtt with mosquitto. and I'm making Qos 2 chat app.

I get a source and Testing these. Qos 0 / 1 Send and Receive are work normally well. but Qos 2 is not good.

Here is my problem.

1) "Publisher" send Qos 2 string "hello" to "Broker". But "Subscriber" dosen't get it. 2) "Publisher" send Qos 2 string "bye" to "Broker". Then "Subscriber" get "hello" message, when i sent before. 3) "Publisher" send Qos 2 string "test" to "Broker". Then "Subscriber" get "bye" message, when i sent before.

I think, "Publisher" can't receive PUBREC message immediately. What is the problem?.... I missed something.

Here is my Test Code.

`#include

include

include "mosquitto.h"

define sleep(x) Sleep((x)*1000)

define strdup _strdup

define DEFAULT_MQTT_HOST "127.0.0.1"

define DEFAULT_MQTT_PORT 1883

define DEFAULT_MQTT_KEEPALIVE 60

define DEFAULT_MQTT_TOPIC "EXAMPLE_TOPIC"

define BUF_LENGTH 65536

using namespace std;

void connect_callback(struct mosquitto mosq, void obj, int result) { printf("connect callback, rc=%d\n", result); }

void message_callback(struct mosquitto mosq, void obj, const struct mosquitto_message msg) { printf("message '%.s' for topic '%s'\n", msg->payloadlen, (char*)msg->payload, msg->topic); }

int main(int argc, char* argv) { int rc; char mqtt_host = strdup(DEFAULT_MQTT_HOST); char* mqtt_topic = strdup(DEFAULT_MQTT_TOPIC); int mqtt_port = DEFAULT_MQTT_PORT; int mqtt_keepalive = DEFAULT_MQTT_KEEPALIVE;

int mdelay = 0;
bool clean_session = true;

struct mosquitto* mosq = NULL;

mosquitto_lib_init();
mosq = mosquitto_new(NULL, clean_session, NULL);
if (!mosq) {
    fprintf(stderr, "Could not create new mosquitto struct\n");
    exit(1);
}

if (mosquitto_connect(mosq, mqtt_host, mqtt_port, mqtt_keepalive)) {
    fprintf(stderr, "Unable to connect mosquitto.\n");
    exit(1);
}

char buf[BUF_LENGTH];

do {
    rc = mosquitto_loop(mosq, 0, 1);

    scanf_s("%s", buf, BUF_LENGTH);
    if (!strcmp(buf, "exit")) break;

    rc = mosquitto_publish(mosq, NULL, mqtt_topic, strlen(buf), buf, 2, 0);
    if (rc != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "Error publishing: %s\n", mosquitto_strerror(rc));
    }
    rc = mosquitto_subscribe(mosq, NULL, mqtt_topic, 2);

} while (rc == MOSQ_ERR_SUCCESS);

mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
free(mqtt_host);
free(mqtt_topic);

return 0;

}`

Erickrk commented 4 months ago

Hmm this looks interesting. Can you please clarify which libraries you're using to publish/subscribe? is this still happening?