-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "blocking" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
blocking = "1.3" | ||
tokio = { version = "1", features = ["full"] } | ||
futures = "0.3" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let fut = blocking::unblock(||{ | ||
loop{ | ||
println!("a"); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
}); | ||
let fut2 = tokio::task::spawn_blocking(||{ | ||
loop{ | ||
println!("b"); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
}); | ||
let fut3 = async { | ||
loop { | ||
println!("c"); | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
} | ||
}; | ||
let fut4 = tokio::time::sleep(std::time::Duration::from_secs(3)); | ||
tokio::select!{ | ||
_ = fut => { | ||
println!("never"); | ||
} | ||
_ = fut2 => { | ||
println!("never"); | ||
} | ||
_ = fut3 => { | ||
println!("never"); | ||
} | ||
_ = fut4 => { | ||
println!("done"); | ||
} | ||
} | ||
tokio::time::sleep(std::time::Duration::from_secs(3)).await; | ||
println!("fin"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "mqtt-pah" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[features] | ||
default = ["paho"] | ||
paho = ["paho-mqtt", "paho-mqtt-sys", "tempfile"] | ||
rumqtt = ["rumqttc", "rustls-pemfile"] | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
futures = "0.3" | ||
log = "0.4" | ||
paho-mqtt = { version = "0.12", features = ["bundled", "build_bindgen"], optional = true } | ||
paho-mqtt-sys = { version = "0.8", optional = true } | ||
rumqttc = { version = "0.22", optional = true } | ||
rustls-pemfile = { version = "1.0", optional = true } | ||
tempfile = { version = "3.6.0", optional = true } | ||
tokio = { version = "1", features = ["full"] } | ||
env_logger = "0.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const mqtt = require('mqtt') | ||
const aedes = require('aedes')() | ||
const server = require('net').createServer(aedes.handle) | ||
const port = 1883 | ||
|
||
const evs = [ | ||
"client", | ||
"clientReady", | ||
"clientDisconnect", | ||
"clientError", | ||
"connectionError", | ||
"keepaliveTimeout", | ||
"publish", | ||
"ack", | ||
"ping", | ||
"subscribe", | ||
"unsubscribe", | ||
"connackSent", | ||
"closed", | ||
// | ||
"error", | ||
"ready", | ||
"new", | ||
]; | ||
for(const ev of evs){ | ||
aedes.on(ev, (...args)=>{ | ||
args.pop(); | ||
console.info.bind(console, "aedes", ev, ...args) | ||
}); | ||
} | ||
aedes.on("client", (client)=>{ | ||
console.info("aedes","client"); | ||
const evs = [ | ||
"connected", | ||
"error", | ||
]; | ||
for(const ev of evs){ | ||
client.on(ev, console.info.bind(console, "aedes client", ev)); | ||
} | ||
}); | ||
|
||
server.listen(port, function () { | ||
console.log('server started and listening on port ', port) | ||
//const mclient = mqtt.connect(`mqtt://127.0.0.1:${port}`); | ||
//const evs = [ | ||
// "connect", | ||
// "reconnect", | ||
// "close", | ||
// "disconnect", | ||
// "offline", | ||
// "error", | ||
// "end", | ||
// "message", | ||
// "packetsend", | ||
// "packetreceive", | ||
//]; | ||
//for(ev of evs){ | ||
// mclient.on(ev, console.info.bind(console, "mqtt", ev)); | ||
//} | ||
|
||
aedes.on("clientReady", (client)=>{ | ||
console.log("aedes clientReady"); | ||
setTimeout(()=>{ | ||
console.log("aedes client closing"); | ||
client.close(()=>{ | ||
console.log("aedes client closed"); | ||
}); | ||
}, 6000); | ||
}); | ||
}); |
Oops, something went wrong.