...
int msgarrived(void *actx, char *topic, int tlen, MQTTClient_message *msg){
/* handle message arrival and call associated function.
* NOTE : up to now, only textual topics & messages are
* correctly handled (lengths are simply ignored)
*/
struct enhanced_client *ctx = actx; /* To avoid numerous cast */
struct _topic *tp;
for(tp = ctx->subscriptions; tp; tp = tp->next){ /* Looks for the corresponding function */
if(!strcmp(tp->topic, topic)){ /* AF : wildcard to be done */
lua_rawgeti( ctx->L, LUA_REGISTRYINDEX, tp->func); /* retrieves the function */
lua_pushstring( ctx->L, topic);
lua_pushstring( ctx->L, msg->payload);
lua_pcall( ctx->L, 2, 0, 0); /* Call Lua callback function */
}
}
MQTTClient_freeMessage(&msg);
MQTTClient_free(topic);
return 1;
}
print '\nTesting broker connection'
print '--------------------------'
-- Callbacks
function handle_tata( topic, msg )
print("Lua received t:'" .. topic .."' m:'".. msg .. "'\n");
end
function handle_toto( topic, msg )
print("Lua received t:'" .. topic .."' m:'".. msg .. "'\n");
end
-- Connection, subscription and finally waiting for messages
Brk, err = SelMQTT.connect( "tcp://localhost:1883", { reliable=false } )
if not Brk then
print( err )
return
end
_, err = Brk:subscribe( { { topic = "/tata/#", func=handle_tata, qos=SelMQTT.QoSConst("QoS0") }, { topic = "/toto",func=handle_toto } } )
if err then
print( err )
return
end
print "Hit 'enter' key to exit"
io.stdin:read'*l' -- wait for enter
And it seems it's working pretty well.
If I enter in another shell :
---
torchwood ~ # mosquitto_pub -h torchwood.local -t '/toto' -m 'Coucou'
---
I got
---
torchwood Selene # Selenites/MQTT.sel
Testing broker connection
--------------------------
Hit 'enter' key to exit
Lua received t:'/toto' m:'Coucou'
---
Can someone having a look and tell me if I made a mistake ?
Now, I'll have to work data shared b/w threads :)
Thanks for all.
Laurent
---