mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 13:01:12 +03:00
Начало положено...
This commit is contained in:
commit
3fd49300db
12 changed files with 2361 additions and 0 deletions
51
src/ws/handler.rs
Normal file
51
src/ws/handler.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use axum::{extract::{ws::{Message, WebSocket}, WebSocketUpgrade}, response::Response};
|
||||
use log::{error, info, warn};
|
||||
|
||||
use crate::ws::C2SMessage;
|
||||
|
||||
pub async fn handler(ws: WebSocketUpgrade) -> Response {
|
||||
ws.on_upgrade(handle_socket)
|
||||
}
|
||||
|
||||
async fn handle_socket(mut socket: WebSocket) {
|
||||
while let Some(msg) = socket.recv().await {
|
||||
info!("{msg:?}");
|
||||
let mut msg = if let Ok(msg) = msg {
|
||||
msg
|
||||
} else {
|
||||
// if reached here - client disconnected
|
||||
warn!("ws disconnected!");
|
||||
return;
|
||||
};
|
||||
// Work with code here
|
||||
let msg_array = msg.clone().into_data();
|
||||
let msg_array = msg_array.as_slice();
|
||||
|
||||
let newmsg = match C2SMessage::try_from(msg_array) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
error!("MessageLoadError: {e:?}");
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
match newmsg {
|
||||
C2SMessage::Token(token) => {
|
||||
// TODO: Authenticated check
|
||||
msg = Message::Binary(vec![0])
|
||||
},
|
||||
// C2SMessage::Ping(_, _, _) => todo!(),
|
||||
// C2SMessage::Sub(_) => todo!(),
|
||||
// C2SMessage::Unsub(_) => todo!(),
|
||||
_ => ()
|
||||
}
|
||||
|
||||
info!("{newmsg:?}");
|
||||
|
||||
if socket.send(msg).await.is_err() {
|
||||
// if reached here - client disconnected
|
||||
warn!("ws disconnected!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue