GithubHelp home page GithubHelp logo

Comments (4)

kitgxrl avatar kitgxrl commented on July 28, 2024

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.

HuakunShen avatar HuakunShen commented on July 28, 2024

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.

HuakunShen avatar HuakunShen commented on July 28, 2024

@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.

shenjackyuanjie avatar shenjackyuanjie commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.