Skip to content

Instantly share code, notes, and snippets.

@anlumo
Created October 8, 2019 02:08
Show Gist options
  • Save anlumo/37005e36db06c6aab788af0837c529a3 to your computer and use it in GitHub Desktop.
Save anlumo/37005e36db06c6aab788af0837c529a3 to your computer and use it in GitHub Desktop.
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