1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Add asynchronous operation in user websocket

This commit is contained in:
Pierre HUBERT 2022-03-11 22:46:13 +01:00
parent f919409634
commit 0a9ff17615

View File

@ -244,7 +244,7 @@ impl WsSession {
}
/// Handle incoming message
fn handle_message(&self, ctx: &mut ws::WebsocketContext<Self>, msg: &str) -> Res<UserWsMessage> {
async fn handle_message(addr: Addr<WsSession>, msg: &str) -> Res<UserWsMessage> {
let incoming_msg: UserWsMessage = serde_json::from_str(&msg)?;
let data = incoming_msg.data.as_object()
@ -262,7 +262,7 @@ impl WsSession {
}
let mut handler = UserWsRequestHandler::new(
&find_connection(ctx.address()).ok_or(ExecError::boxed_new("Connection not found!"))?,
&find_connection(addr).ok_or(ExecError::boxed_new("Connection not found!"))?,
args,
);
@ -357,7 +357,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
}
ws::Message::Text(msg) => {
match self.handle_message(ctx, &msg) {
let addr = ctx.address();
let future = async move {
match Self::handle_message(addr.clone(), &msg).await {
Ok(msg) => {
let response = serde_json::to_string(&msg)
.unwrap_or("Failed to serialize".to_string());
@ -366,14 +368,16 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
println!("USER WEBSOCKET RESPONSE {}", response);
}
ctx.text(response)
addr.do_send(WsQueuedMessage(response))
}
Err(e) => {
println!("WS processing error: {}", e);
ctx.text("Failed to parse message");
addr.do_send(WsQueuedMessage("Failed to parse message".to_string()));
}
}
};
future.into_actor(self).spawn(ctx);
}
ws::Message::Binary(_) => {