1

I have maybe a weird question but I need to know this. Im creating a lua script to connect esp8266 and my mqtt broker. The example script is very straight forward.

m = mqtt.Client("clientid", 120, "user", "password")

m:lwt("/lwt", "offline", 0, 0)

m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)

m:on("message", function(conn, topic, data) 
  print(topic .. ":" ) 
  if data ~= nil then
    print(data)
  end
end)

m:connect("192.168.11.118", 1880, 0, function(conn) 
    print("connected") 
end)

m:subscribe("/topic",0, function(conn) 
    print("subscribe success") 
end)

m:publish("/topic","hello",0,0, function(conn) 
    print("sent") 
end)

m:close();

But... there is one thing I can't get over. That is the "con" and "conn" parameter? It looks like an instance or something, but there is no such a thing defined. Can somebody explain this to me?

4

1 回答 1

1

文档中您可以发现客户端的on()方法注册了一个回调,其中第一个参数是客户端本身。为了您的方便(如果链接失效),我复制了以下相关信息:

mqtt.client:on()

描述

将回调函数注册到事件。

句法

mqtt:on(event, function(client, [topic], [message]))

参数

event
字符串,可以是:“connect”、“message”、“offline”

function(client, [topic], [message])
回调函数。第一个参数是客户端。如果事件是“消息”,则第二个和第三个参数以字符串形式接收主题和消息。

退货

零。

于 2015-08-24T13:22:37.007 回答