diff --git a/src/chat/front_conn.rs b/src/chat/front_conn.rs index 2dfd7a8..ab925e3 100644 --- a/src/chat/front_conn.rs +++ b/src/chat/front_conn.rs @@ -4,11 +4,11 @@ use async_std::{ net::{TcpListener, TcpStream}, task, }; -use async_tungstenite::{ - accept_async_with_config, tungstenite::protocol::WebSocketConfig, tungstenite::Message, - WebSocketStream, +use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream}; +use futures::{ + stream::{SplitSink, SplitStream}, + SinkExt, StreamExt, }; -use futures::{SinkExt, StreamExt}; use serde_derive::Deserialize; use std::env; use std::sync::mpsc::{Receiver, Sender}; @@ -16,6 +16,14 @@ use std::sync::mpsc::{Receiver, Sender}; type SP = Sender; type RP = Receiver; +#[derive(Deserialize, Debug)] +struct FrontMsg { + user_id: u32, + receiver_id: u32, + message: String, + time: String, +} + pub fn listen_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> { task::block_on(connect_to_client(server_sender, to_client_receiver)) } @@ -28,86 +36,43 @@ async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Res let listener = TcpListener::bind(&addr).await?; if let Ok((stream, _)) = listener.accept().await { - let ss = server_sender.clone(); - let srm = stream.clone(); - let t1 = task::spawn(accept_client(srm, ss, to_client_receiver)); - // let t2 = task::spawn(respond_to_client(stream, to_client_receiver)); - t1.await; - // t2.await; + let addr = stream + .peer_addr() + .expect("connected streams should have a peer address"); + println!("Peer address: {}", addr); + + let ws = accept_async(stream) + .await + .expect("err during the ws handshake"); + let (sender, receiver) = ws.split(); + println!("connected to: {}", addr); + + let t1 = task::spawn(connection_for_sending(receiver, server_sender)); + connection_for_receiving(sender, to_client_receiver).await?; + t1.await?; } Ok(()) } -async fn respond_to_client(stream: TcpStream, to_client_receiver: RP) -> io::Result<()> { - while let Ok(res) = to_client_receiver.recv() { - println!("From Server!:\n {}", res.message); - } - Ok(()) -} - -#[derive(Deserialize, Debug)] -struct FrontMsg { - userID: u32, - receiverID: u32, - message: String, - time: String, -} - -async fn accept_client( - stream: TcpStream, - server_sender: SP, - to_client_receiver: RP, -) -> io::Result<()> { - let addr = stream - .peer_addr() - .expect("connected streams should have a peer address"); - println!("Peer address: {}", addr); - - let cfg = WebSocketConfig { - max_send_queue: None, - max_message_size: Some(67108864), - max_frame_size: Some(16777216), - }; - // let ws = accept_async_with_config(stream, Some(cfg.clone())) - // .await - // .expect("err during the ws handshake"); - // println!("connected to: {}", addr); - - // let wsr = ws.get_ref().to_owned(); - // let wsr2 = ws.get_ref().to_owned(); - - Ok(()) -} - async fn connection_for_receiving( - wsr: TcpStream, + mut sender: SplitSink, Message>, to_client_receiver: RP, - cfg: WebSocketConfig, ) -> io::Result<()> { - let (sc, _) = (accept_async_with_config(wsr, Some(cfg)) - .await - .expect("error during the clone handshake")) - .split() - .unwrap(); - + while let Ok(res) = to_client_receiver.recv() { + println!("HEYNEHHEFSJDHFKLSDJ \n {}", res.message); + sender + .send(Message::Text(String::from(res.message).to_owned())) + .await + .expect("ooops"); + } Ok(()) } async fn connection_for_sending( - ws: TcpStream, + mut receiver: SplitStream>, server_sender: SP, - cfg: WebSocketConfig, ) -> io::Result<()> { - let (mut sender, mut receiver) = (accept_async_with_config(ws, Some(cfg)) - .await - .expect("error during the clone handshake")) - .split(); - /* - while let Ok(res) = to_client_receiver.recv() { - //sc.send(Message::Text(res.message.to_string())); - } - */ let mut new_msg = receiver.next(); loop { match new_msg.await { @@ -119,18 +84,14 @@ async fn connection_for_sending( let msg = received_msg.message; server_sender.send(PackedMessage { message: msg }).unwrap(); - /* message example - { - "userID": 123456789, - "receiverID": 123456789, - "message": "hey dude", - "time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)" - } - */ - sender - .send(Message::Text(String::from("Sended").to_owned())) - .await - .expect("ooops"); + /* message example + { + "userID": 123456789, + "receiverID": 123456789, + "message": "hey dude", + "time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)" + } + */ } else { println!("seems, that messsage formatted wrong"); println!("{:?}", res); @@ -143,6 +104,5 @@ async fn connection_for_sending( } } } - Ok(()) }