Comments (4)
This can be fixed by an additional clone:
let tx_clone = tx.clone();
let callback = |payload: Payload, socket: Client| {
let tx_clone = tx_clone.clone();
async move {
tx_clone.send("...".to_string());
}.boxed()
};
let callback2 = |payload: Payload, socket: Client| {
let tx = tx.clone();
async move {
tx.send("...".to_string());
}.boxed()
};
this is untested but how I recall fixing this. It's not by any means ideal but #363 is likely related
from rust-socketio.
Thanks! I figured this out.
But what I ended up doing is use on_any
and channel to transfer all event handling to another loop outside the closures to avoid variables moving. It's much simpler.
Here is how I did it.
#[derive(Debug)]
pub struct EventMessage {
pub event: String,
pub payload: Payload,
}
let (done_tx, mut done_rx) = tokio::sync::mpsc::channel::<()>(1);
let (evt_tx, mut evt_rx) = tokio::sync::mpsc::channel::<EventMessage>(1);
let socket = ClientBuilder::new(SERVER_URL)
.on(Event::Connect, |_, _| async move {}.boxed())
.on_any(move |evt, payload, _| {
let evt_tx = evt_tx.clone();
async move {
evt_tx
.send(EventMessage {
event: evt.to_string(),
payload,
})
.await
.unwrap();
}
.boxed()
})
.on(Event::Error, |err, _: Client| {
async move {
eprintln!("Error: {:#?}", err);
}
.boxed()
})
.connect()
.await
.expect("Connection failed");
loop {
tokio::select! {
_ = done_rx.recv() => {
break;
}
Some(evt) = evt_rx.recv() => {
// Handle event received from evt_rx
match evt.event.as_str() {
"evt1" => {...}
"evt2" => {...}
}
}
_ = tokio::signal::ctrl_c() => {
break;
}
};
}
from rust-socketio.
@1c3t3a
I wonder if it's possible to update the design to use channels for event handling rather than closures. The socket.connect().await
can simply return a channel + a socket, then we can read events and payloads directly from the channel to avoid dealing with closures and cloning and move
variables to heap.
from rust-socketio.
about this, I'm also thinking about another thing, how to share a "status data" between different callback function?
like in axum
you can use a State
to share state between callbacks
how can I do that over here?
from rust-socketio.
Related Issues (20)
- EngineIO does not timeout on missing pings
- Event handler lifetime shouldn't be static HOT 4
- Add feature to use rustls instead of native-tls HOT 1
- Buffer is not supported when it's a field in an JS object? HOT 2
- Manual disconnect doesn't block reconnecting HOT 1
- Add support for WebTransport HOT 2
- add macro to simplyfy async callback define HOT 4
- Allow async connections to update auth when reconnecting HOT 4
- [feature request] custom-parser support HOT 5
- Unable to cross-compile for android HOT 1
- Multiple payload still broke HOT 2
- when using binary buf data with emit_with_ack function callback is not executed
- Is it possible to add `.on()` event callback after connect like in JavaScript? HOT 1
- Binary Data event names incorrectly handled HOT 1
- Introduce Custom shared data structure
- [bug] unable to connect to an http based socketio server HOT 2
- Help
- Does it support rooms in SocketIO ? HOT 1
- Async client is not notified of (unexpected) websocket transport close
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-socketio.