Created
October 8, 2019 02:08
-
-
Save anlumo/37005e36db06c6aab788af0837c529a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub async fn run(mut self) -> Result<(), Box<dyn std::error::Error>> { | |
loop { | |
match select(self.stream.next(), self.channel.next()).await { | |
Either::Left((Some(Ok(message)), _)) => { | |
match message { | |
Message::Text(txt) => { | |
debug!("[{}] Received message: {:?}", self.uuid, txt); | |
}, | |
Message::Binary(bin) => { | |
debug!("[{}] Received binary message: {:?}", self.uuid, bin); | |
}, | |
Message::Ping(data) => { | |
self.stream.send(Message::Pong(data)).await.map_err(Box::new)?; | |
}, | |
Message::Pong(_) => {}, | |
Message::Close(reason) => { | |
debug!("[{}] Connection closed: {:?}", self.uuid, reason); | |
}, | |
} | |
}, | |
Either::Left((Some(Err(err)), _)) => { | |
error!("[{}] {}", self.uuid, err); | |
return Err(Box::new(err) as Box<dyn std::error::Error>); | |
}, | |
Either::Right((Some(message), _)) => { | |
match message { | |
Message::Close(reason) => { | |
debug!("[{}] Close connection: {:?}", self.uuid, reason); | |
self.stream.send(Message::Close(reason)).await.map_err(Box::new)?; | |
return Ok(()); | |
}, | |
msg => { | |
debug!("[{}] Sending message: {:?}", self.uuid, msg); | |
self.stream.send(msg).await.map_err(Box::new)?; | |
}, | |
} | |
}, | |
_ => { | |
return Ok(()); | |
}, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment