So I'm using the Actix framework to create a simple websocket game server. The idea is that there's a random number and the server will go around and poll each user to get their guess. I've tweaked an actix example of a websocket chat server to get the basics going such as handling connects, disconnects, and a handler for when a guess is submitted.
However, I'm running into an issue when I want to poll the connected clients and wait for their response. It seems like the best way to do this would be to use send for Actix Recipients which sends a message and waits for a response. It returns a RecipientResponse which is a future. From here I'm not sure what I should be doing. Would this be chaining and_thens on the future to poll each user and take their guess in order? Essentially get a reponse from the future -> process the guess -> poll the next user -> etc....
Here's an example of the super basic main loop I have:
fn prompt_user(&self, message: &str, id: usize) -> RecipientResponse<M>{
if let Some((addr, _)) = &self.sessions.get(&id) {
let resp = addr.send(Message(message.to_owned()));
resp
}
}
.....
.....
fn play_game(&mut self) {
//sessions: hashmap of usize -> Recipient
for (id, addr) in &self.sessions {
let current_action = format!("Action is on: {}", id );
self.send_message(¤t_action, *id);
let resp = self.prompt_user("place guess!", *id);
}
}
I might be going about this incorrectly but any help would be appreciated.