Skip to content

Commit

Permalink
send a message every second
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Oct 17, 2017
1 parent 3333416 commit 1a0bf16
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <lwmqtt/unix.h>

#define COMMAND_TIMEOUT 5000
#define MESSAGE_TIMEOUT 1000

static void message_arrived(lwmqtt_client_t *client, void *ref, lwmqtt_string_t topic, lwmqtt_message_t msg) {
printf("message_arrived: %.*s => %.*s (%d)\n", (int)topic.len, topic.data, (int)msg.payload_len, (char *)msg.payload,
Expand All @@ -13,7 +14,7 @@ static void message_arrived(lwmqtt_client_t *client, void *ref, lwmqtt_string_t

int main() {
lwmqtt_unix_network_t network;
lwmqtt_unix_timer_t timer1, timer2;
lwmqtt_unix_timer_t timer1, timer2, timer3;

lwmqtt_client_t client;

Expand All @@ -23,6 +24,8 @@ int main() {
lwmqtt_set_timers(&client, &timer1, &timer2, lwmqtt_unix_timer_set, lwmqtt_unix_timer_get);
lwmqtt_set_callback(&client, NULL, message_arrived);

lwmqtt_unix_timer_set(NULL, &timer3, MESSAGE_TIMEOUT);

lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, "broker.shiftr.io", 1883);
if (err != LWMQTT_SUCCESS) {
printf("failed lwmqtt_unix_network_connect: %d\n", err);
Expand Down Expand Up @@ -71,5 +74,17 @@ int main() {
printf("failed lwmqtt_keep_alive: %d\n", err);
exit(1);
}

if (lwmqtt_unix_timer_get(NULL, &timer3) <= 0) {
lwmqtt_message_t msg = {.qos = LWMQTT_QOS0, .retained = false, .payload = (uint8_t *)("world"), .payload_len = 5};

err = lwmqtt_publish(&client, lwmqtt_string("hello"), msg, COMMAND_TIMEOUT);
if (err != LWMQTT_SUCCESS) {
printf("failed lwmqtt_keep_alive: %d\n", err);
exit(1);
}

lwmqtt_unix_timer_set(NULL, &timer3, MESSAGE_TIMEOUT);
}
}
}

0 comments on commit 1a0bf16

Please sign in to comment.