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 /// 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 incoming_msg: UserWsMessage = serde_json::from_str(&msg)?;
let data = incoming_msg.data.as_object() let data = incoming_msg.data.as_object()
@ -262,7 +262,7 @@ impl WsSession {
} }
let mut handler = UserWsRequestHandler::new( 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, args,
); );
@ -357,23 +357,27 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
} }
ws::Message::Text(msg) => { ws::Message::Text(msg) => {
match self.handle_message(ctx, &msg) { let addr = ctx.address();
Ok(msg) => { let future = async move {
let response = serde_json::to_string(&msg) match Self::handle_message(addr.clone(), &msg).await {
.unwrap_or("Failed to serialize".to_string()); Ok(msg) => {
let response = serde_json::to_string(&msg)
.unwrap_or("Failed to serialize".to_string());
if conf().verbose_mode { if conf().verbose_mode {
println!("USER WEBSOCKET RESPONSE {}", response); println!("USER WEBSOCKET RESPONSE {}", response);
}
addr.do_send(WsQueuedMessage(response))
} }
ctx.text(response) Err(e) => {
println!("WS processing error: {}", e);
addr.do_send(WsQueuedMessage("Failed to parse message".to_string()));
}
} }
};
Err(e) => { future.into_actor(self).spawn(ctx);
println!("WS processing error: {}", e);
ctx.text("Failed to parse message");
}
}
} }
ws::Message::Binary(_) => { ws::Message::Binary(_) => {