[FIX] Websocket & webserver is now actix, because that should be better
than hyper & axum, i guess we'll see in the long run :}, ToDo: IF THIS WORKS: implement on the iota
This commit is contained in:
parent
5cfdadd291
commit
cb7579d86d
7 changed files with 1432 additions and 961 deletions
|
|
@ -1,19 +1,16 @@
|
|||
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
|
||||
use crate::server::omikron_manager;
|
||||
use crate::server::short_link::add_short_link;
|
||||
use crate::server::socket::{WsSendMessage, WsSession};
|
||||
use crate::sql::connection_status::UserStatus;
|
||||
use crate::sql::sql::{self, get_by_user_id, get_by_username, get_iota_by_id, get_omikron_by_id};
|
||||
use crate::sql::user_online_tracker::{self};
|
||||
use crate::util::crypto_helper::encrypt;
|
||||
use crate::util::logger::PrintType;
|
||||
use crate::{get_private_key, get_public_key, log_err, log_in, log_out};
|
||||
use axum::extract::ws::WebSocket;
|
||||
use axum::extract::ws::{Message, Utf8Bytes};
|
||||
use crate::{get_private_key, get_public_key, log_in, log_out};
|
||||
use actix::Addr;
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use dashmap::DashMap;
|
||||
use futures::stream::SplitSink;
|
||||
use futures::stream::SplitStream;
|
||||
use futures_util::SinkExt;
|
||||
use json::JsonValue;
|
||||
use json::number::Number;
|
||||
use rand::Rng;
|
||||
|
|
@ -24,8 +21,7 @@ use uuid::Uuid;
|
|||
use x448::PublicKey;
|
||||
|
||||
pub struct OmikronConnection {
|
||||
pub sender: Arc<RwLock<SplitSink<WebSocket, Message>>>,
|
||||
pub receiver: Arc<RwLock<SplitStream<WebSocket>>>,
|
||||
pub ws_addr: Arc<RwLock<Addr<WsSession>>>,
|
||||
pub omikron_id: Arc<RwLock<i64>>,
|
||||
pub pub_key: Arc<RwLock<Option<Vec<u8>>>>,
|
||||
identified: Arc<RwLock<bool>>,
|
||||
|
|
@ -39,13 +35,9 @@ pub struct OmikronConnection {
|
|||
}
|
||||
|
||||
impl OmikronConnection {
|
||||
pub fn new(
|
||||
sender: SplitSink<WebSocket, Message>,
|
||||
receiver: SplitStream<WebSocket>,
|
||||
) -> Arc<Self> {
|
||||
pub fn new(ws_addr: Addr<WsSession>) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
sender: Arc::new(RwLock::new(sender)),
|
||||
receiver: Arc::new(RwLock::new(receiver)),
|
||||
ws_addr: Arc::new(RwLock::new(ws_addr)),
|
||||
omikron_id: Arc::new(RwLock::new(0)),
|
||||
pub_key: Arc::new(RwLock::new(None)),
|
||||
identified: Arc::new(RwLock::new(false)),
|
||||
|
|
@ -56,25 +48,20 @@ impl OmikronConnection {
|
|||
})
|
||||
}
|
||||
pub async fn send_message(&self, cv: &CommunicationValue) {
|
||||
let mut sender = self.sender.write().await;
|
||||
let message_text = Message::Text(Utf8Bytes::from(cv.to_json().to_string()));
|
||||
let text = cv.to_json().to_string();
|
||||
|
||||
if !cv.is_type(CommunicationType::pong) {
|
||||
log_out!(
|
||||
*self.omikron_id.read().await,
|
||||
PrintType::Omikron,
|
||||
"{}",
|
||||
cv.to_json().to_string()
|
||||
);
|
||||
}
|
||||
if let Err(e) = sender.send(message_text).await {
|
||||
log_err!(
|
||||
*self.omikron_id.read().await,
|
||||
PrintType::Omikron,
|
||||
"WebSocket send error: {}",
|
||||
e
|
||||
text
|
||||
);
|
||||
}
|
||||
|
||||
self.ws_addr.read().await.do_send(WsSendMessage(text));
|
||||
}
|
||||
|
||||
pub async fn get_omikron_id(&self) -> i64 {
|
||||
*self.omikron_id.read().await
|
||||
}
|
||||
|
|
@ -948,7 +935,6 @@ impl OmikronConnection {
|
|||
self.send_message(&error).await;
|
||||
}
|
||||
pub async fn close(&self) {
|
||||
let mut sender = self.sender.write().await;
|
||||
if self.is_identified().await {
|
||||
let omikron_id = self.get_omikron_id().await;
|
||||
if omikron_id != 0 {
|
||||
|
|
@ -957,7 +943,6 @@ impl OmikronConnection {
|
|||
user_online_tracker::untrack_omikron(omikron_id).await;
|
||||
}
|
||||
}
|
||||
let _ = sender.close().await;
|
||||
}
|
||||
pub async fn handle_close(self: Arc<Self>) {
|
||||
if self.is_identified().await {
|
||||
|
|
|
|||
Loading…
Reference in a new issue