Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMPORTANT) QoS 2 Receive is weired #2625

Open
Jisean opened this issue Sep 2, 2022 · 1 comment
Open

IMPORTANT) QoS 2 Receive is weired #2625

Jisean opened this issue Sep 2, 2022 · 1 comment

Comments

@Jisean
Copy link

Jisean commented Sep 2, 2022

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 <Windows.h>
#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
Copy link

Erickrk commented Apr 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants