finished chat client (ws part)

This commit is contained in:
Franklin 2023-02-28 20:12:37 -04:00
parent ea8807faf6
commit ae6694bba6
7 changed files with 125 additions and 49 deletions

130
Cargo.lock generated
View File

@ -132,9 +132,9 @@ dependencies = [
[[package]] [[package]]
name = "actix-web" name = "actix-web"
version = "4.3.0" version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "464e0fddc668ede5f26ec1f9557a8d44eda948732f40c6b0ad79126930eb775f" checksum = "cd3cb42f9566ab176e1ef0b8b3a896529062b4efc6be0123046095914c4c1c96"
dependencies = [ dependencies = [
"actix-codec", "actix-codec",
"actix-http", "actix-http",
@ -167,15 +167,15 @@ dependencies = [
"serde_urlencoded", "serde_urlencoded",
"smallvec", "smallvec",
"socket2", "socket2",
"time 0.3.19", "time 0.3.20",
"url", "url",
] ]
[[package]] [[package]]
name = "actix-web-codegen" name = "actix-web-codegen"
version = "4.1.0" version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa9362663c8643d67b2d5eafba49e4cb2c8a053a29ed00a0bea121f17c76b13" checksum = "2262160a7ae29e3415554a3f1fc04c764b1540c116aa524683208078b7a75bc9"
dependencies = [ dependencies = [
"actix-router", "actix-router",
"proc-macro2", "proc-macro2",
@ -297,9 +297,9 @@ checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a"
[[package]] [[package]]
name = "base64ct" name = "base64ct"
version = "1.5.3" version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]] [[package]]
name = "bincode" name = "bincode"
@ -399,19 +399,20 @@ checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
[[package]] [[package]]
name = "bytecheck" name = "bytecheck"
version = "0.6.9" version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d11cac2c12b5adc6570dad2ee1b87eff4955dac476fe12d81e5fdd352e52406f" checksum = "13fe11640a23eb24562225322cd3e452b93a3d4091d62fab69c70542fcd17d1f"
dependencies = [ dependencies = [
"bytecheck_derive", "bytecheck_derive",
"ptr_meta", "ptr_meta",
"simdutf8",
] ]
[[package]] [[package]]
name = "bytecheck_derive" name = "bytecheck_derive"
version = "0.6.9" version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e576ebe98e605500b3c8041bb888e966653577172df6dd97398714eb30b9bf" checksum = "e31225543cb46f81a7e224762764f4a6a0f097b1db0b175f69e8065efaa42de5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -478,7 +479,7 @@ dependencies = [
[[package]] [[package]]
name = "chat-types" name = "chat-types"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.franklinblanco.dev/franklinblanco/chat-types.git#f6ea7041a775c93f05bdd07bf3a38c8983664cce" source = "git+https://git.franklinblanco.dev/franklinblanco/chat-types.git#ba88d60cf6ccf44841ae0ccf9f2bcd2ad68a7ebd"
dependencies = [ dependencies = [
"chrono", "chrono",
"dev-dtos", "dev-dtos",
@ -554,7 +555,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb" checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb"
dependencies = [ dependencies = [
"percent-encoding", "percent-encoding",
"time 0.3.19", "time 0.3.20",
"version_check", "version_check",
] ]
@ -619,9 +620,9 @@ dependencies = [
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.14" version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
] ]
@ -765,6 +766,27 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "event-listener" name = "event-listener"
version = "2.5.3" version = "2.5.3"
@ -949,9 +971,9 @@ dependencies = [
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.15" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f9f29bc9dda355256b2916cf526ab02ce0aeaaaf2bad60d65ef3f12f11dd0f4" checksum = "5be7b54589b581f624f566bf5d8eb2bab1db736c51528720b6bd36b96b55924d"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -1141,6 +1163,16 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "io-lifetimes"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1abeb7a0dd0f8181267ff8adc397075586500b81b28a73e8a0208b00fc170fb3"
dependencies = [
"libc",
"windows-sys 0.45.0",
]
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.7.1" version = "2.7.1"
@ -1164,9 +1196,9 @@ checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]] [[package]]
name = "jobserver" name = "jobserver"
version = "0.1.25" version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b" checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -1228,6 +1260,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
[[package]] [[package]]
name = "local-channel" name = "local-channel"
version = "0.1.3" version = "0.1.3"
@ -1744,15 +1782,6 @@ version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "rend" name = "rend"
version = "0.4.0" version = "0.4.0"
@ -1886,6 +1915,20 @@ dependencies = [
"semver", "semver",
] ]
[[package]]
name = "rustix"
version = "0.36.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys 0.45.0",
]
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.20.8" version = "0.20.8"
@ -2053,6 +2096,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "simdutf8"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
[[package]] [[package]]
name = "siphasher" name = "siphasher"
version = "0.3.10" version = "0.3.10"
@ -2224,15 +2273,15 @@ dependencies = [
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.4.1" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.107" version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2241,16 +2290,15 @@ dependencies = [
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.3.0" version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" checksum = "af18f7ae1acd354b992402e9ec5864359d693cd8a79dcbef59f76891701c1e95"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"fastrand", "fastrand",
"libc",
"redox_syscall", "redox_syscall",
"remove_dir_all", "rustix",
"winapi", "windows-sys 0.42.0",
] ]
[[package]] [[package]]
@ -2295,9 +2343,9 @@ dependencies = [
[[package]] [[package]]
name = "time" name = "time"
version = "0.3.19" version = "0.3.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53250a3b3fed8ff8fd988587d8925d26a83ac3845d9e03b220b37f34c2b8d6c2" checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890"
dependencies = [ dependencies = [
"itoa", "itoa",
"serde", "serde",
@ -2313,9 +2361,9 @@ checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
[[package]] [[package]]
name = "time-macros" name = "time-macros"
version = "0.2.7" version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a460aeb8de6dcb0f381e1ee05f1cd56fcf5a5f6eb8187ff3d8f0b11078d38b7c" checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36"
dependencies = [ dependencies = [
"time-core", "time-core",
] ]

View File

@ -50,7 +50,12 @@ impl<'a> WebsocketCaller {
let _ = rt.block_on( let _ = rt.block_on(
add_message_to_queue(&self.message_queue, ServerMessageIn::SendMessage(ChatMessageSender { message: ChatMessageContent::Text(message), to })) add_message_to_queue(&self.message_queue, ServerMessageIn::SendMessage(ChatMessageSender { message: ChatMessageContent::Text(message), to }))
); );
}
pub fn see_messages(&self, messages: Vec<u32>,) {
let rt = Runtime::new().unwrap();
let _ = rt.block_on(
add_message_to_queue(&self.message_queue, ServerMessageIn::SeeMessages(messages))
);
} }
} }

View File

@ -11,9 +11,6 @@ use super::utils::interpret_message;
pub async fn handle_message(message: Message, ws_caller: &Arc<Box<dyn WebsocketFfi>>) -> Result<(), Box<dyn std::error::Error + Send + Sync>>{ pub async fn handle_message(message: Message, ws_caller: &Arc<Box<dyn WebsocketFfi>>) -> Result<(), Box<dyn std::error::Error + Send + Sync>>{
let server_message_out = interpret_message(message)?; let server_message_out = interpret_message(message)?;
match server_message_out { match server_message_out {
ServerMessageOut::Acknowledge => {
todo!()
},
ServerMessageOut::LoggedIn => ws_caller.logged_in()?, ServerMessageOut::LoggedIn => ws_caller.logged_in()?,
ServerMessageOut::MessageSent => ws_caller.message_sent()?, ServerMessageOut::MessageSent => ws_caller.message_sent()?,
ServerMessageOut::MessageRecieved(message) => ws_caller.message_recieved(message.into())?, ServerMessageOut::MessageRecieved(message) => ws_caller.message_recieved(message.into())?,

15
src/client/chat/http.rs Normal file
View File

@ -0,0 +1,15 @@
use chat_types::{dto::chat::ChatRoomParticipants, client_types::chat_room::ChatRoom};
use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto;
use reqwest::Client;
use tokio::runtime::Runtime;
use crate::{RustError, unwrap_rust_error};
pub fn create_new_chat_room(user: UserForAuthenticationDto, participants: ChatRoomParticipants, title: String) -> Result<ChatRoom, RustError> {
let rt = Runtime::new().unwrap();
let client = Client::new();
Ok(unwrap_rust_error!(rt.block_on(
chat_communicators::client::chat::create_new_chat_room(&client, &user, &participants, &title)
)))
}

View File

@ -1,4 +1,4 @@
pub mod utils; pub mod handler; pub mod utils; pub mod handler; pub mod http;
use std::{sync::{Arc}, time::Duration}; use std::{sync::{Arc}, time::Duration};
@ -91,9 +91,11 @@ where S: futures_util::Sink<tokio_tungstenite::tungstenite::Message> + Unpin {
let mut write_lock_for_queue = message_queue.write().await; let mut write_lock_for_queue = message_queue.write().await;
if messages_to_remove_from_queue.len() > 0 { if messages_to_remove_from_queue.len() > 0 {
for i in messages_to_remove_from_queue { for i in messages_to_remove_from_queue {
if write_lock_for_queue.len() >= i + 1 {
write_lock_for_queue.remove(i); write_lock_for_queue.remove(i);
} }
} }
} }
}
// Do nothing // Do nothing
} }

View File

@ -6,6 +6,7 @@ pub mod utils;
pub mod callbacks; pub mod callbacks;
pub use chat_types::client_types::chat_room::ChatRoom; pub use chat_types::client_types::chat_room::ChatRoom;
pub use chat_types::dto::chat::ChatRoomParticipants;
pub use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto; pub use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto;
use utils::storage; use utils::storage;
pub use utils::storage::*; pub use utils::storage::*;
@ -13,7 +14,7 @@ pub use callbacks::chat::*;
pub use league_types::domain::sport::Sport; pub use league_types::domain::sport::Sport;
pub use types::error::*; pub use types::error::*;
//pub use chat_communicators::client::chat::*; pub use client::chat::http::*;
pub use chat_types::client_types::chat_message::*; pub use chat_types::client_types::chat_message::*;
pub fn get_all_sports() -> Result<Vec<Sport>, RustError> { pub fn get_all_sports() -> Result<Vec<Sport>, RustError> {
@ -24,11 +25,12 @@ pub fn get_me() -> Result<UserForAuthenticationDto, RustError> {
storage::read("user".into()) storage::read("user".into())
} }
#[macro_export] #[macro_export]
macro_rules! unwrap_rust_error { macro_rules! unwrap_rust_error {
($e:expr) => { ($e:expr) => {
match $e { match $e {
Ok(result) => result, Ok(result) => result.into(),
Err(error) => return Err(error.into()) Err(error) => return Err(error.into())
} }
}; };

View File

@ -53,6 +53,9 @@ dictionary ChatRoom {
i64 last_updated; i64 last_updated;
u64 session_messages; u64 session_messages;
}; };
dictionary ChatRoomParticipants {
sequence<u32> participants;
};
enum ClientError { enum ClientError {
"One", "One",
"Two", "Two",
@ -79,11 +82,15 @@ interface WebsocketCaller {
constructor(); constructor();
void init_ws_connection(WebsocketFfi websocket_ffi); void init_ws_connection(WebsocketFfi websocket_ffi);
void send_text_message(string message, u32 to); void send_text_message(string message, u32 to);
void see_messages(sequence<u32> messages);
}; };
namespace network { namespace network {
[Throws=RustError] [Throws=RustError]
UserForAuthenticationDto get_me(); UserForAuthenticationDto get_me();
[Throws=RustError] [Throws=RustError]
sequence<Sport> get_all_sports(); sequence<Sport> get_all_sports();
void init_storage(string path); void init_storage(string path);
[Throws=RustError]
ChatRoom create_new_chat_room(UserForAuthenticationDto user, ChatRoomParticipants participants, string title);
}; };