Skip to content

Commit d954084

Browse files
committed
chore(examples) fix copas examples, remove sync example
1 parent 3f4433a commit d954084

File tree

3 files changed

+71
-99
lines changed

3 files changed

+71
-99
lines changed

examples/copas-example.lua

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
local mqtt = require("mqtt")
44
local copas = require("copas")
5-
local mqtt_ioloop = require("mqtt.ioloop")
65

76
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
109

1110
-- NOTE: more about flespi tokens: https://flespi.com/kb/tokens-access-keys-to-flespi-platform
1211
local token = "stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9"
@@ -16,31 +15,35 @@ local ping = mqtt.client{
1615
username = token,
1716
clean = true,
1817
version = mqtt.v50,
18+
-- NOTE: copas connector
19+
connector = require("mqtt.luasocket-copas"),
1920
}
2021

2122
local pong = mqtt.client{
2223
uri = "mqtt.flespi.io",
2324
username = token,
2425
clean = true,
2526
version = mqtt.v50,
27+
-- NOTE: copas connector
28+
connector = require("mqtt.luasocket-copas"),
2629
}
2730

2831
ping:on{
2932
connect = function(connack)
3033
assert(connack.rc == 0)
3134
print("ping connected")
3235

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)
4447
end,
4548
error = function(err)
4649
print("ping MQTT client error:", err)
@@ -72,20 +75,39 @@ pong:on{
7275
end,
7376
}
7477

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
82100

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...")
88102

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)
89111
copas.loop()
90112

91113
print("done, copas loop is stopped")

examples/copas.lua

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,31 @@ client:on{
5555
end
5656
}
5757

58-
-- run io loop for client until connection close
59-
copas.addthread(function()
60-
print("running client in separated copas thread #1...")
61-
mqtt.run_sync(client)
6258

63-
-- NOTE: in sync mode no automatic reconnect is working, but you may just wrap "mqtt.run_sync(client)" call in a loop like this:
64-
-- while true do
65-
-- mqtt.run_sync(client)
66-
-- end
67-
end)
59+
local function add_client(cl)
60+
-- add keep-alive timer
61+
local timer = copas.addthread(function()
62+
while cl do
63+
copas.sleep(cl:check_keep_alive())
64+
end
65+
end)
66+
-- add client to connect and listen
67+
copas.addthread(function()
68+
while cl do
69+
local timeout = cl:step()
70+
if not timeout then
71+
cl = nil -- exiting
72+
copas.wakeup(timer)
73+
else
74+
if timeout > 0 then
75+
copas.sleep(timeout)
76+
end
77+
end
78+
end
79+
end)
80+
end
6881

69-
copas.addthread(function()
70-
print("execution of separated copas thread #2...")
71-
copas.sleep(0.1)
72-
print("thread #2 stopped")
73-
end)
7482

83+
add_client(client)
7584
copas.loop()
7685
print("done, copas loop is stopped")

examples/sync.lua

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)