style: use ? operator instead of unwrap in websockets
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
This commit is contained in:
parent
6640d03b70
commit
5d26f94cdd
2 changed files with 19 additions and 15 deletions
|
@ -62,49 +62,51 @@ pub async fn echo(
|
||||||
let mut session_2 = session_1.clone();
|
let mut session_2 = session_1.clone();
|
||||||
|
|
||||||
rt::spawn(async move {
|
rt::spawn(async move {
|
||||||
pubsub.subscribe(channel_uuid.to_string()).await.unwrap();
|
pubsub.subscribe(channel_uuid.to_string()).await?;
|
||||||
while let Some(msg) = pubsub.on_message().next().await {
|
while let Some(msg) = pubsub.on_message().next().await {
|
||||||
let payload: String = msg.get_payload().unwrap();
|
let payload: String = msg.get_payload()?;
|
||||||
session_1.text(payload).await.unwrap();
|
session_1.text(payload).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok::<(), crate::error::Error>(())
|
||||||
});
|
});
|
||||||
|
|
||||||
// start task but don't wait for it
|
// start task but don't wait for it
|
||||||
rt::spawn(async move {
|
rt::spawn(async move {
|
||||||
let mut conn = data
|
|
||||||
.cache_pool
|
|
||||||
.get_multiplexed_tokio_connection()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
// receive messages from websocket
|
// receive messages from websocket
|
||||||
while let Some(msg) = stream.next().await {
|
while let Some(msg) = stream.next().await {
|
||||||
match msg {
|
match msg {
|
||||||
Ok(AggregatedMessage::Text(text)) => {
|
Ok(AggregatedMessage::Text(text)) => {
|
||||||
// echo text message
|
let mut conn = data
|
||||||
|
.cache_pool
|
||||||
|
.get_multiplexed_tokio_connection()
|
||||||
|
.await?;
|
||||||
|
|
||||||
redis::cmd("PUBLISH")
|
redis::cmd("PUBLISH")
|
||||||
.arg(&[channel_uuid.to_string(), text.to_string()])
|
.arg(&[channel_uuid.to_string(), text.to_string()])
|
||||||
.exec_async(&mut conn)
|
.exec_async(&mut conn)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
channel
|
channel
|
||||||
.new_message(&mut data.pool.get().await.unwrap(), uuid, text.to_string())
|
.new_message(&mut data.pool.get().await.unwrap(), uuid, text.to_string())
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(AggregatedMessage::Binary(bin)) => {
|
Ok(AggregatedMessage::Binary(bin)) => {
|
||||||
// echo binary message
|
// echo binary message
|
||||||
session_2.binary(bin).await.unwrap();
|
session_2.binary(bin).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(AggregatedMessage::Ping(msg)) => {
|
Ok(AggregatedMessage::Ping(msg)) => {
|
||||||
// respond to PING frame with PONG frame
|
// respond to PING frame with PONG frame
|
||||||
session_2.pong(&msg).await.unwrap();
|
session_2.pong(&msg).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok::<(), crate::error::Error>(())
|
||||||
});
|
});
|
||||||
|
|
||||||
// respond immediately with response connected to WS session
|
// respond immediately with response connected to WS session
|
||||||
|
|
|
@ -52,6 +52,8 @@ pub enum Error {
|
||||||
UrlParseError(#[from] url::ParseError),
|
UrlParseError(#[from] url::ParseError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
PayloadError(#[from] PayloadError),
|
PayloadError(#[from] PayloadError),
|
||||||
|
#[error(transparent)]
|
||||||
|
WsClosed(#[from] actix_ws::Closed),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
PasswordHashError(String),
|
PasswordHashError(String),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue