1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-25 13:08:50 +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,23 +357,27 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
}
ws::Message::Text(msg) => {
match self.handle_message(ctx, &msg) {
Ok(msg) => {
let response = serde_json::to_string(&msg)
.unwrap_or("Failed to serialize".to_string());
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());
if conf().verbose_mode {
println!("USER WEBSOCKET RESPONSE {}", response);
if conf().verbose_mode {
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) => {
println!("WS processing error: {}", e);
ctx.text("Failed to parse message");
}
}
};
future.into_actor(self).spawn(ctx);
}
ws::Message::Binary(_) => {