first commit
This commit is contained in:
11
examples/01_publish.jl
Normal file
11
examples/01_publish.jl
Normal file
@@ -0,0 +1,11 @@
|
||||
using Mosquitto, Dates
|
||||
|
||||
# connect to a broker, also start loop if Threads.nthreads() > 1
|
||||
client = Client("test.mosquitto.org")
|
||||
|
||||
topic = "test/julia"
|
||||
message = "Hello World from Julia, send on $(now(UTC)) using the Mosquitto wrapper https://github.com/denglerchr/Mosquitto.jl"
|
||||
publish(client, topic, message; retain = true)
|
||||
!client.status.loop_status && loop(client; ntimes = 2)
|
||||
|
||||
disconnect(client)
|
||||
32
examples/02_subscribe_simple.jl
Normal file
32
examples/02_subscribe_simple.jl
Normal file
@@ -0,0 +1,32 @@
|
||||
# Read 20 messages in topic "test/..." from the public broker test.mosquitto.org
|
||||
using Mosquitto
|
||||
|
||||
# Connect to a broker, also starts loop if Threads.nthreads()>1
|
||||
client = Client("test.mosquitto.org", 1883)
|
||||
|
||||
# subscribe to topic "test"
|
||||
subscribe(client, "test/#")
|
||||
|
||||
function onmessage(nmin)
|
||||
nmessages = Base.n_avail(Mosquitto.messages_channel)
|
||||
nmessages == 0 && return 0
|
||||
|
||||
for i = 1:nmessages
|
||||
temp = take!(Mosquitto.messages_channel)
|
||||
println("Message $(nmin + i) of 20:")
|
||||
println("\ttopic: $(temp.topic)\tmessage:$(String(temp.payload))")
|
||||
end
|
||||
return nmessages
|
||||
end
|
||||
|
||||
# Messages will be put in
|
||||
# the channel Mosquitto.messages_channel.
|
||||
nmessages = 0
|
||||
while nmessages<20
|
||||
# Take the message on arrival
|
||||
loop(client)
|
||||
nmessages += onmessage(nmessages)
|
||||
end
|
||||
|
||||
# Close everything
|
||||
disconnect(client)
|
||||
29
examples/02_subscribe_simple_threaded.jl
Normal file
29
examples/02_subscribe_simple_threaded.jl
Normal file
@@ -0,0 +1,29 @@
|
||||
# Read 20 messages in topic "test/..." from the public broker test.mosquitto.org
|
||||
# This example assumes julia was started with >1 thread
|
||||
# e.g., julia -t 2 subscribe.jl
|
||||
if Threads.nthreads()<2
|
||||
println("Start julia using atleast 2 threads to run this example:")
|
||||
println("julia -t 2 subscribe.jl")
|
||||
exit(1)
|
||||
end
|
||||
|
||||
using Mosquitto
|
||||
|
||||
# Connect to a broker, also starts loop if Threads.nthreads()>1
|
||||
client = Client("test.mosquitto.org", 1883)
|
||||
|
||||
# subscribe to topic "test"
|
||||
subscribe(client, "test/#")
|
||||
|
||||
# Messages will be put in
|
||||
# the channel Mosquitto.messages_channel.
|
||||
for i = 1:20
|
||||
# Take the message on arrival
|
||||
temp = take!(Mosquitto.messages_channel)
|
||||
# Do something with the message
|
||||
println("Message $i of 20:")
|
||||
println("\ttopic: $(temp.topic)\tmessage:$(String(temp.payload))")
|
||||
end
|
||||
|
||||
# Close everything
|
||||
disconnect(client)
|
||||
56
examples/03_subscribe_onconnect.jl
Normal file
56
examples/03_subscribe_onconnect.jl
Normal file
@@ -0,0 +1,56 @@
|
||||
# Read 20 messages in topic "test/..." from the public broker test.mosquitto.org
|
||||
# Different from example 02, the client will resubscribe to its topic every time it connects to the broker
|
||||
using Mosquitto
|
||||
|
||||
# Connect to a broker, but dont start network loop.
|
||||
# We will trigger the network loop manually here using the loop function
|
||||
client = Client("test.mosquitto.org", 1883; startloop = false)
|
||||
|
||||
# subscribe to topic "test" every time the client connects
|
||||
function onconnect(c)
|
||||
# Check if something happened, else return 0
|
||||
nmessages = Base.n_avail(get_connect_channel())
|
||||
nmessages == 0 && return 0
|
||||
|
||||
# At this point, a connection or disconnection happened
|
||||
for i = 1:nmessages
|
||||
conncb = take!(get_connect_channel())
|
||||
if conncb.val == 1
|
||||
println("Connection of client $(conncb.clientptr) successfull, subscribing to test/#")
|
||||
subscribe(c, "test/#")
|
||||
elseif conncb.val == 0
|
||||
println("Client $(conncb.clientptr) disconnected")
|
||||
end
|
||||
end
|
||||
return nmessages
|
||||
end
|
||||
|
||||
|
||||
function onmessage(mrcount)
|
||||
# Check if something happened, else return 0
|
||||
nmessages = Base.n_avail(get_messages_channel())
|
||||
nmessages == 0 && return 0
|
||||
|
||||
# At this point, a message was received, lets process it
|
||||
for i = 1:nmessages
|
||||
temp = take!(get_messages_channel())
|
||||
println("Message $(mrcount+i):")
|
||||
message = String(temp.payload)
|
||||
length(message) > 20 && (message = message[1:18]*"...")
|
||||
println("\ttopic: $(temp.topic)\tmessage:$(message)")
|
||||
end
|
||||
return nmessages
|
||||
end
|
||||
|
||||
|
||||
# Messages will be put as a tuple in
|
||||
# the channel Mosquitto.messages_channel.
|
||||
mrcount = 0
|
||||
while mrcount < 20
|
||||
loop(client) # network loop
|
||||
onconnect(client) # check for connection/disconnection
|
||||
mrcount += onmessage(mrcount) # check for messages
|
||||
end
|
||||
|
||||
# Close everything
|
||||
disconnect(client)
|
||||
47
examples/03_subscribe_onconnect_threaded.jl
Normal file
47
examples/03_subscribe_onconnect_threaded.jl
Normal file
@@ -0,0 +1,47 @@
|
||||
# Read 20 messages in topic "test/..." from the public broker test.mosquitto.org
|
||||
# Different from example 02, the client will resubscribe to its topic every time it connects to the broker
|
||||
# This example assumes julia was started with >1 thread
|
||||
# e.g., julia -t 2 subscribe.jl
|
||||
if Threads.nthreads()<2
|
||||
println("Start julia using atleast 2 threads to run this example:")
|
||||
println("julia -t 2 subscribe.jl")
|
||||
exit(1)
|
||||
end
|
||||
|
||||
using Mosquitto
|
||||
|
||||
# Connect to a broker, also starts loop if Threads.nthreads()>1
|
||||
client = Client("test.mosquitto.org", 1883)
|
||||
|
||||
# subscribe to topic "test" every time the client connects
|
||||
function subonconnect(c)
|
||||
while true
|
||||
conncb = take!(get_connect_channel())
|
||||
if conncb.val == 1
|
||||
println("Connection of client $(conncb.clientptr) successfull, subscribing to test/#")
|
||||
subscribe(c, "test/#")
|
||||
elseif conncb.val == 0
|
||||
println("Client $(conncb.clientptr) disconnected")
|
||||
else
|
||||
println("Subonconnect function returning")
|
||||
return 0
|
||||
end
|
||||
end
|
||||
end
|
||||
Threads.@spawn subonconnect(client)
|
||||
|
||||
# Messages will be put as a tuple in
|
||||
# the channel Mosquitto.messages_channel.
|
||||
for i = 1:50
|
||||
# Take the message on arrival
|
||||
temp = take!(get_messages_channel())
|
||||
# Do something with the message
|
||||
println("Message $i of 50:")
|
||||
message = String(temp.payload)
|
||||
length(message) > 15 && (message = message[1:13]*"...")
|
||||
println("\ttopic: $(temp.topic)\tmessage:$(message)")
|
||||
end
|
||||
|
||||
# Close everything
|
||||
put!(Mosquitto.connect_channel, Mosquitto.ConnectionCB(Ptr{Mosquitto.Cmosquitto}(C_NULL), UInt8(255), 0))
|
||||
disconnect(client)
|
||||
68
examples/04_multiple_clients.jl
Normal file
68
examples/04_multiple_clients.jl
Normal file
@@ -0,0 +1,68 @@
|
||||
# Connect to 3 clients, 1 on the test server of mosquitto and 2 on localhost (requires a broker to run on localhost:1883)
|
||||
# We run this only with manual loop execution, as multiple client with threaded network loop are currently not supported.
|
||||
# What this script does:
|
||||
# The client 1 will subscribe to messages, and every time a message is received in the correct topic test/julia
|
||||
# client 2 will publish a message to localhost which is again received by client 3
|
||||
|
||||
using Mosquitto
|
||||
|
||||
# Connect to a broker, also starts loop if Threads.nthreads()>1
|
||||
client1 = Client("test.mosquitto.org", 1883, startloop = false) # will receive message
|
||||
client2 = Client("localhost", 1883, startloop = false) # will publish to localhost
|
||||
client3 = Client("localhost", 1883, startloop = false) # will receive from localhost
|
||||
|
||||
# subscribe to topic different topics for each client
|
||||
function subonconnect(c1::Client, c2::Client, c3::Client)
|
||||
# check channel, if there is something todo
|
||||
nmessages = Base.n_avail(get_connect_channel())
|
||||
nmessages == 0 && return 0
|
||||
for i = 1:nmessages
|
||||
conncb = take!(get_connect_channel())
|
||||
if conncb.val == 1
|
||||
println("$(conncb.clientptr): connection successfull")
|
||||
conncb.clientptr == c1.cptr.mosc && subscribe(c1, "test/#")
|
||||
conncb.clientptr == c3.cptr.mosc && subscribe(c3, "julia")
|
||||
elseif conncb.val == 0
|
||||
println("$(conncb.clientptr): disconnected")
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
# What to do if there is a message
|
||||
function onmessage(c1, c2)
|
||||
rand()<0.1 && publish(c1, "test/julia", "From client 1"; retain = false)
|
||||
|
||||
nmessages = Base.n_avail(get_messages_channel())
|
||||
nmessages == 0 && return 0
|
||||
for i = 1:nmessages
|
||||
temp = take!(get_messages_channel())
|
||||
# Do something with the message
|
||||
if temp.topic == "test/julia"
|
||||
println("\ttopic: $(temp.topic)\tmessage:$(String(temp.payload))")
|
||||
publish(c2, "julia", "From client 2"; qos = 2)
|
||||
elseif temp.topic == "julia"
|
||||
println("\ttopic: $(temp.topic)\tmessage:$(String(temp.payload))")
|
||||
else
|
||||
println("Wrong topic :(")
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
# Messages will be put as a Message struct
|
||||
# the channel Mosquitto.messages_channel.
|
||||
for i = 1:200
|
||||
loop(client1; timeout = 100)
|
||||
loop(client2; timeout = 100)
|
||||
loop(client3; timeout = 100)
|
||||
|
||||
subonconnect(client1, client2, client3)
|
||||
onmessage(client1, client2)
|
||||
end
|
||||
|
||||
|
||||
# Close everything
|
||||
disconnect(client1)
|
||||
disconnect(client2)
|
||||
disconnect(client3)
|
||||
26
examples/05_tls_cert.jl
Normal file
26
examples/05_tls_cert.jl
Normal file
@@ -0,0 +1,26 @@
|
||||
using Mosquitto
|
||||
|
||||
# defined paths to cafile, certfile, keyfile
|
||||
include("authfiles/certpaths.jl")
|
||||
|
||||
# Create client, but dont connect yet
|
||||
client = Client()
|
||||
|
||||
# Configure tls by providing crt and key files, needs to be done before connecting
|
||||
tls_set(client, cafile; certfile = certfile, keyfile = keyfile)
|
||||
|
||||
# Connect
|
||||
connect(client, "test.mosquitto.org", 8884)
|
||||
|
||||
# Rest as usual, subscribe and publish and read messages
|
||||
subscribe(client, "test")
|
||||
publish(client, "test/julia", "hello"; retain = false)
|
||||
client.status.loop_status ? sleep(1) : loop(client; ntimes = 10)
|
||||
|
||||
nmessages = Base.n_avail(Mosquitto.messages_channel)
|
||||
for i = 1:nmessages
|
||||
msg = take!(Mosquitto.messages_channel) # Tuple{String, Vector{UInt8})
|
||||
println("Topic: $(msg.topic)\tMessage: $(String(msg.payload))")
|
||||
end
|
||||
|
||||
disconnect(client)
|
||||
26
examples/06_tls_pw.jl
Normal file
26
examples/06_tls_pw.jl
Normal file
@@ -0,0 +1,26 @@
|
||||
using Mosquitto
|
||||
|
||||
# import paths to the server ca file (can be downloaded from https://test.mosquitto.org/ )
|
||||
include("authfiles/certpaths.jl")
|
||||
|
||||
# Create client, but dont connect yet
|
||||
client = Client()
|
||||
|
||||
# Configure tls using the ca certificate, needs to be done before connecting
|
||||
tls_set(client, cafile)
|
||||
|
||||
# Connect using username and password
|
||||
connect(client, "test.mosquitto.org", 8885; username = "rw", password = "readwrite")
|
||||
|
||||
# Rest as usual, subscribe and publish and read messages
|
||||
subscribe(client, "test")
|
||||
publish(client, "test/julia", "hello"; retain = false)
|
||||
client.status.loop_status ? sleep(1) : loop(client; ntimes = 10)
|
||||
|
||||
nmessages = Base.n_avail(Mosquitto.messages_channel)
|
||||
for i = 1:nmessages
|
||||
msg = take!(Mosquitto.messages_channel) # Tuple{String, Vector{UInt8})
|
||||
println("Topic: $(msg.topic)\tMessage: $(String(msg.payload))")
|
||||
end
|
||||
|
||||
disconnect(client)
|
||||
Reference in New Issue
Block a user