finish part of hole core for communicating with frontend

This commit is contained in:
Horhik 2021-01-09 00:02:45 +02:00
parent c9c79a4bc6
commit c48739d836

View file

@ -4,11 +4,11 @@ use async_std::{
net::{TcpListener, TcpStream}, net::{TcpListener, TcpStream},
task, task,
}; };
use async_tungstenite::{ use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
accept_async_with_config, tungstenite::protocol::WebSocketConfig, tungstenite::Message, use futures::{
WebSocketStream, stream::{SplitSink, SplitStream},
SinkExt, StreamExt,
}; };
use futures::{SinkExt, StreamExt};
use serde_derive::Deserialize; use serde_derive::Deserialize;
use std::env; use std::env;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
@ -16,6 +16,14 @@ use std::sync::mpsc::{Receiver, Sender};
type SP = Sender<PackedMessage>; type SP = Sender<PackedMessage>;
type RP = Receiver<PackedMessage>; type RP = Receiver<PackedMessage>;
#[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<()> { pub fn listen_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender, to_client_receiver)) 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?; let listener = TcpListener::bind(&addr).await?;
if let Ok((stream, _)) = listener.accept().await { if let Ok((stream, _)) = listener.accept().await {
let ss = server_sender.clone(); let addr = stream
let srm = stream.clone(); .peer_addr()
let t1 = task::spawn(accept_client(srm, ss, to_client_receiver)); .expect("connected streams should have a peer address");
// let t2 = task::spawn(respond_to_client(stream, to_client_receiver)); println!("Peer address: {}", addr);
t1.await;
// t2.await; 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(()) 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( async fn connection_for_receiving(
wsr: TcpStream, mut sender: SplitSink<WebSocketStream<TcpStream>, Message>,
to_client_receiver: RP, to_client_receiver: RP,
cfg: WebSocketConfig,
) -> io::Result<()> { ) -> io::Result<()> {
let (sc, _) = (accept_async_with_config(wsr, Some(cfg)) while let Ok(res) = to_client_receiver.recv() {
.await println!("HEYNEHHEFSJDHFKLSDJ \n {}", res.message);
.expect("error during the clone handshake")) sender
.split() .send(Message::Text(String::from(res.message).to_owned()))
.unwrap(); .await
.expect("ooops");
}
Ok(()) Ok(())
} }
async fn connection_for_sending( async fn connection_for_sending(
ws: TcpStream, mut receiver: SplitStream<WebSocketStream<TcpStream>>,
server_sender: SP, server_sender: SP,
cfg: WebSocketConfig,
) -> io::Result<()> { ) -> 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(); let mut new_msg = receiver.next();
loop { loop {
match new_msg.await { match new_msg.await {
@ -119,18 +84,14 @@ async fn connection_for_sending(
let msg = received_msg.message; let msg = received_msg.message;
server_sender.send(PackedMessage { message: msg }).unwrap(); server_sender.send(PackedMessage { message: msg }).unwrap();
/* message example /* message example
{ {
"userID": 123456789, "userID": 123456789,
"receiverID": 123456789, "receiverID": 123456789,
"message": "hey dude", "message": "hey dude",
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)" "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");
} else { } else {
println!("seems, that messsage formatted wrong"); println!("seems, that messsage formatted wrong");
println!("{:?}", res); println!("{:?}", res);
@ -143,6 +104,5 @@ async fn connection_for_sending(
} }
} }
} }
Ok(()) Ok(())
} }