static void ev_handler(struct mg_connection *nc, int ev, void *p) { struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p; (void) nc; #if 0 if (ev != MG_EV_POLL) printf("USER HANDLER GOT %d\n", ev); #endif switch (ev) { case MG_EV_CONNECT: { struct mg_send_mqtt_handshake_opts opts; memset(&opts, 0, sizeof(opts)); opts.user_name = s_user_name; opts.password = s_password; mg_set_protocol_mqtt(nc); mg_send_mqtt_handshake_opt(nc, "dummy", opts); break; } case MG_EV_MQTT_CONNACK: if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) { printf("Got mqtt connection error: %d\n", msg->connack_ret_code); exit(1); } s_topic_expr.topic = s_topic; printf("Subscribing to '%s'\n", s_topic); mg_mqtt_subscribe(nc, &s_topic_expr, 1, 42); break; case MG_EV_MQTT_PUBACK: printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id); break; case MG_EV_MQTT_SUBACK: printf("Subscription acknowledged, forwarding to '/test'\n"); break; case MG_EV_MQTT_PUBLISH: { #if 0 char hex[1024] = {0}; mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex)); printf("Got incoming message %.*s:\n%s", (int)msg->topic.len, msg->topic.p, hex); #else printf("Got incoming message %.*s: %.*s\n", (int) msg->topic.len, msg->topic.p, (int) msg->payload.len, msg->payload.p); #endif printf("Forwarding to /test\n"); mg_mqtt_publish(nc, "/test", 65, MG_MQTT_QOS(0), msg->payload.p, msg->payload.len); break; } case MG_EV_CLOSE: printf("Connection closed\n"); exit(1); } }
static void ev_handler(struct mg_connection *nc, int ev, void *p) { struct mg_mqtt_message *msg = (struct mg_mqtt_message *)p; (void) nc; #if 0 if (ev != NS_POLL) printf("USER HANDLER GOT %d\n", ev); #endif switch (ev) { case NS_CONNECT: mg_set_protocol_mqtt(nc); mg_send_mqtt_handshake(nc, "dummy"); break; case NS_MQTT_CONNACK: if (msg->connack_ret_code != NS_MQTT_CONNACK_ACCEPTED) { printf("Got mqtt connection error: %d\n", msg->connack_ret_code); exit(1); } printf("Subscribing to '/stuff'\n"); mg_mqtt_subscribe(nc, topic_expressions, sizeof(topic_expressions)/sizeof(*topic_expressions), 42); break; case NS_MQTT_PUBACK: printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id); break; case NS_MQTT_SUBACK: printf("Subscription acknowledged, forwarding to '/test'\n"); break; case NS_MQTT_PUBLISH: { #if 0 char hex[1024] = {0}; mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex)); printf("Got incoming message %s:\n%s", msg->topic, hex); #else printf("Got incoming message %s: %.*s\n", msg->topic, (int)msg->payload.len, msg->payload.p); #endif printf("Forwarding to /test\n"); mg_mqtt_publish(nc, "/test", 65, NS_MQTT_QOS(0), msg->payload.p, msg->payload.len); } break; case NS_CLOSE: printf("Connection closed\n"); exit(1); } }