Skip to content

Commit

Permalink
Addition of graceful method to disconnect (#34)
Browse files Browse the repository at this point in the history
* addition of graceful method to disconnect

* Fix to error grammar

* Change to disconnect output to return null on successful execution
  • Loading branch information
cmccarthy1 authored Jul 1, 2020
1 parent ba4ef3f commit 37fb3a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion q/mqtt.q
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
\d .mqtt
connX :`mqtt 2:(`connX;3);
connX:`mqtt 2:(`connX;3);
init :`mqtt 2:(`init;1);
pubx :`mqtt 2:(`pub ;4);
sub :`mqtt 2:(`sub ;1);
unsub:`mqtt 2:(`unsub;1);
isConnected:`mqtt 2:(`isConnected;1);
disconnect :`mqtt 2:(`disconnect;1);


pub:.mqtt.pubx[;;1;0]
conn:{[tcpconn;pname;opt]connX[tcpconn;pname](enlist[`]!enlist(::)),opt}
Expand Down
22 changes: 22 additions & 0 deletions src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ EXP K connX(K tcpconn,K pname, K opt){
return (K)0;
}

/* Disconnect from an mqtt client
* timeout = length of time in ms to allow for the client to clean up prior to disconnection
*/

EXP K disconnect(K timeout){
if(!MQTTClient_isConnected(client))
return krr((S)"No client is currently connected");
else{
MQTTClient_disconnect(client,(time_t)timeout->i);
MQTTClient_destroy(&client);
}
return (K)0;
}

EXP K isConnected(K UNUSED(x)){
if(!MQTTClient_isConnected(client))
return kb(0);
else
return kb(1);
}


/* Publish a message to a specified topic
* topic = topic name as a symbol
* msg = message content as a string
Expand Down

0 comments on commit 37fb3a9

Please sign in to comment.