-
Hey everyone! I am currently building a game server, and I'm trying to architect a system that can perform the following logic:
In order to perform this logic, I think I need to inject a clone of the SocketIo struct into the room logic handler like so: pub async fn raid_room_server(io: SocketIo, room_id: String) {
loop {
let _ = io
.to(room_id.clone())
.emit("raid-even", "Hello from the raid room you joined!"); // Handle updating state based on user inputs and AI movements, and the emit the changes to the client every 100 ms
sleep(Duration::from_millis(100)).await;
}
} Now inside this tokio task, I can concurrently handle a room session while emitting events (though I have not figured out how to get future socket events of players connected to the room. So here if the problematic lines of code: socket.on(
"join_raid",
|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(io, data.raid_id)); // right now 'io' does not exist because I don't know how to inject it into this closure
info!("{:?}", socket.rooms());
},
); I'm very curious if anyone has come across this type of problem and have a better solution than the one I suggested, or if it is a decent solution what is the remedy? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello ! Indeed you cannot inject the 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!("{:?}", socket.rooms());
},
); We clone the |
Beta Was this translation helpful? Give feedback.
Hello ! Indeed you cannot inject the
io
struct in a handler with extractors. But what you can do is using themove
keyword on your closure declaration to move theio
ref from the parent scope. See: https://doc.rust-lang.org/std/keyword.move.htmlIn your case: