Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mosquitto » OnConnect() is not getting called. (Though mosquitto_connect() is executing successfully, OnConnect callback is not getting called.)
OnConnect() is not getting called. [message #1815736] Wed, 09 October 2019 13:48 Go to next message
Chinmaya Panigrahi is currently offline Chinmaya PanigrahiFriend
Messages: 1
Registered: October 2019
Junior Member
/* Even though other callbacks are getting called like OnDisconnect() or OnPublish(). Return value from OnDisconnect is 7 which means "Connection Lost". I do not know how to deal with it, would anyone please like to help me ?*/
static void onDisconnect(struct mosquitto *mosq, void *obj, int rc){
std::cout<<__FUNCTION__<<" : "<<__LINE__<<" : "<<mosquitto_strerror(rc)<<std::endl;
cout << "disconnect status..." << rc << endl;
// mosquitto_loop_stop(mosq, false); (void)obj, (void)mosq;
}


static void onConnect(struct mosquitto *mosq, void *userdata, int result){
std::cout<<__FUNCTION__<<" : "<<__LINE__<<" : mosquitto connect callback succeed. \n "<<std::endl;
}


static void onCallback(struct mosquitto *mosq, void *userdata, int level, const char *str){
// Pring all log messages regardless of level. (void)mosq, (void)userdata, (void)level; std::cout<<__FUNCTION__<<" : "<<str<<std::endl;
}

mosquitto_lib_init();
struct mosquitto * mosq = NULL;
const char *clientId = "ClientId";
mosq = mosquitto_new(clientId, true, NULL);

if (!mosq) {
std::cout<<__FUNCTION__<<" : "<<__LINE__<<" mosquitto_new failed. \n "<<std::endl;
return;
}else
std::cout<<__FUNCTION__<<" : "<<__LINE__<<" mosquitto_new succeed. \n "<<std::endl;

mosquitto_tls_opts_set(mosq, 1, NULL, NULL);
mosquitto_tls_set(mosq, "/etc/ssl/certs/ca-certificates.crt", NULL,
"/opt/usr_data/tests/LoaderPoc/certs/cert.pem",
"/opt/usr_data/tests/LoaderPoc/certs/privkey.pem", NULL);

mosquitto_connect_callback_set(mosq, onConnect);
mosquitto_disconnect_callback_set(mosq, onDisconnect);
mosquitto_log_callback_set(mosq, onCallback);
//mosquitto_publish_callback_set(mosq, onPublish);

const char *host = "a2eg3przjv23yh.iot.us-east-1.amazonaws.com";
auto ret = mosquitto_connect(mosq, host, 8883, 60);
cout << "connection status.." << ret <<endl;

auto loop_ret = mosquitto_loop_start(mosq);
cout << "Loop status..." << loop_ret << endl;
Re: OnConnect() is not getting called. [message #1815790 is a reply to message #1815736] Thu, 10 October 2019 19:59 Go to previous message
Roger Light is currently offline Roger LightFriend
Messages: 47
Registered: September 2013
Member
It doesn't look like this is the full code, your program would run mosquitto_loop_start() then exit immediately. It's difficult to say what is happening without a full example I'm afraid.
Previous Topic:flow data bteween SQL Data base and SmartPhoen via MQTT broker
Next Topic:Is it permitted to publish in message callback?
Goto Forum:
  


Current Time: Sat Nov 28 20:20:38 GMT 2020

Powered by FUDForum. Page generated in 0.02148 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top