Skip to content

Injecting a .clone() of the SocketIo struct into the on_connect(). #293

Answered by Totodore
JustLeif asked this question in Q&A
Discussion options

You must be logged in to vote

Hello ! Indeed you cannot inject the io struct in a handler with extractors. But what you can do is using the move keyword on your closure declaration to move the io ref from the parent scope. See: https://doc.rust-lang.org/std/keyword.move.html

In your case:

let io1 = io.clone();
socket.on(
        "join_raid",
        move |socket: SocketRef, Data::<JoinRaidSocketEvent>(data), Bin(bin)| async move {
            info!("Received event: {:?} {:?}", data, bin);
            info!("Log the Socket: {:?}", socket);
            let _ = socket.leave_all();
            let _ = socket.join([data.raid_id]);
            let handler = tokio::spawn(raid_room_server(io1, data.raid_id));
            info!(

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JustLeif
Comment options

Answer selected by Totodore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants