2
2
3
3
local mqtt = require (" mqtt" )
4
4
local copas = require (" copas" )
5
- local mqtt_ioloop = require (" mqtt.ioloop" )
6
5
7
6
local num_pings = 10 -- total number of ping-pongs
8
- local timeout = 1 -- timeout between ping-pongs
9
- local suffix = tostring (math.random (1000000 )) -- mqtt topic suffix to distinct simultaneous rinning of this script
7
+ local delay = 1 -- delay between ping-pongs
8
+ local suffix = tostring (math.random (1000000 )) -- mqtt topic suffix to distinct simultaneous running of this script
10
9
11
10
-- NOTE: more about flespi tokens: https://flespi.com/kb/tokens-access-keys-to-flespi-platform
12
11
local token = " stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9"
@@ -16,31 +15,35 @@ local ping = mqtt.client{
16
15
username = token ,
17
16
clean = true ,
18
17
version = mqtt .v50 ,
18
+ -- NOTE: copas connector
19
+ connector = require (" mqtt.luasocket-copas" ),
19
20
}
20
21
21
22
local pong = mqtt .client {
22
23
uri = " mqtt.flespi.io" ,
23
24
username = token ,
24
25
clean = true ,
25
26
version = mqtt .v50 ,
27
+ -- NOTE: copas connector
28
+ connector = require (" mqtt.luasocket-copas" ),
26
29
}
27
30
28
31
ping :on {
29
32
connect = function (connack )
30
33
assert (connack .rc == 0 )
31
34
print (" ping connected" )
32
35
33
- for i = 1 , num_pings do
34
- copas . sleep ( timeout )
35
- print ( " ping " , i )
36
- assert ( ping : publish { topic = " luamqtt/copas- ping/ " .. suffix , payload = " ping " .. i , qos = 1 } )
37
- end
38
-
39
- copas . sleep ( timeout )
40
-
41
- print ( " ping done" )
42
- assert ( ping :publish { topic = " luamqtt/copas-ping/ " .. suffix , payload = " done " , qos = 1 } )
43
- ping : disconnect ( )
36
+ copas . addthread ( function ()
37
+ for i = 1 , num_pings do
38
+ copas . sleep ( delay )
39
+ print ( " ping" , i )
40
+ assert ( ping : publish { topic = " luamqtt/copas-ping/ " .. suffix , payload = " ping " .. i , qos = 1 })
41
+ end
42
+
43
+ print ( " ping done " )
44
+ assert ( ping : publish { topic = " luamqtt/copas- ping/ " .. suffix , payload = " done" , qos = 1 } )
45
+ ping :disconnect ( )
46
+ end )
44
47
end ,
45
48
error = function (err )
46
49
print (" ping MQTT client error:" , err )
@@ -72,20 +75,39 @@ pong:on{
72
75
end ,
73
76
}
74
77
75
- print (" running copas loop..." )
76
-
77
- copas .addthread (function ()
78
- local ioloop = mqtt_ioloop .create { sleep = 0.01 , sleep_function = copas .sleep }
79
- ioloop :add (ping )
80
- ioloop :run_until_clients ()
81
- end )
78
+ local function add_client (cl )
79
+ -- add keep-alive timer
80
+ local timer = copas .addthread (function ()
81
+ while cl do
82
+ copas .sleep (cl :check_keep_alive ())
83
+ end
84
+ end )
85
+ -- add client to connect and listen
86
+ copas .addthread (function ()
87
+ while cl do
88
+ local timeout = cl :step ()
89
+ if not timeout then
90
+ cl = nil -- exiting
91
+ copas .wakeup (timer )
92
+ else
93
+ if timeout > 0 then
94
+ copas .sleep (timeout )
95
+ end
96
+ end
97
+ end
98
+ end )
99
+ end
82
100
83
- copas .addthread (function ()
84
- local ioloop = mqtt_ioloop .create { sleep = 0.01 , sleep_function = copas .sleep }
85
- ioloop :add (pong )
86
- ioloop :run_until_clients ()
87
- end )
101
+ print (" running copas loop..." )
88
102
103
+ add_client (ping )
104
+ add_client (pong )
105
+ -- copas.addthread(function()
106
+ -- for i = 1,5 do
107
+ -- print "hi"
108
+ -- copas.sleep(1)
109
+ -- end
110
+ -- end)
89
111
copas .loop ()
90
112
91
113
print (" done, copas loop is stopped" )
0 commit comments