-
Hello ! I've hesitated before coming ask this question, but after many unsuccessful attempts I finally could use a hint. Most of the crate usage is very straightforward since similar concepts with tower, axum etc but I couldn't understand if there's a way to share a State (e.g. with some connection, so usually an I've tried different ways (especially
What did I miss here ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Here's a sample of what I try to achieve, built from examples and people's feedback on other discussions (especially this one): pub(crate) async fn launch(state: Arc<State>) -> Result<(), anyhow::Error> {
let uri = "127.0.0.1:8883";
let state = state.clone();
let _ = ntex::server::Server::build()
.bind("mqtt", uri, move |_| {
let state = state.clone();
MqttServer::new()
.v3(
v3::MqttServer::new(handshake_v3).publish(fn_factory_with_config(
move |_: v3::Session<Session>| async move {
// let state = state.clone(); // 👈 as soon as uncommented, yields a compiler error
Ok::<_, crate::error::Error>(fn_service(move |publish: v3::Publish| {
// let state = state.clone().client;
// publish_v3(publish, client)
// ^^^^^^ where client is, e.g. influxdb2::Client
ok::<(), crate::error::Error>(())
}))
},
)),
)
.v5(v5::MqttServer::new(handshake_v5).publish(publish_v5))
})?
.workers(1)
.run()
.await;
Ok(())
} Does not seem that everything has to implement Some help would be very much appreciated 😅 |
Beta Was this translation helpful? Give feedback.
-
Well, so big rookie mistakes that costed me hours, here's how it finally compiles: pub(crate) async fn launch(state: Arc<State>) -> Result<(), anyhow::Error> {
let uri = "127.0.0.1:8883";
let state = state.clone();
let _ = ntex::server::Server::build()
.bind("mqtt", uri, move |_| {
let state = state.clone();
MqttServer::new()
.v3(
v3::MqttServer::new(handshake_v3).publish(fn_factory_with_config(
move |_: v3::Session<Session>| {
let state = state.clone();
ok::<_, crate::error::Error>(fn_service(move |publish: v3::Publish| {
let client = state.clone().client.clone();
publish_v3(publish, client)
}))
},
)),
)
.v5(v5::MqttServer::new(handshake_v5).publish(publish_v5))
})?
.workers(1)
.run()
.await;
Ok(())
} Pretty intense. |
Beta Was this translation helpful? Give feedback.
Well, so big rookie mistakes that costed me hours, here's how it finally compiles: