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},
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<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<()> {
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;
}
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 ws = accept_async(stream)
.await
.expect("err during the ws handshake");
let (sender, receiver) = ws.split();
println!("connected to: {}", addr);
// let wsr = ws.get_ref().to_owned();
// let wsr2 = ws.get_ref().to_owned();
let t1 = task::spawn(connection_for_sending(receiver, server_sender));
connection_for_receiving(sender, to_client_receiver).await?;
t1.await?;
}
Ok(())
}
async fn connection_for_receiving(
wsr: TcpStream,
mut sender: SplitSink<WebSocketStream<TcpStream>, Message>,
to_client_receiver: RP,
cfg: WebSocketConfig,
) -> io::Result<()> {
let (sc, _) = (accept_async_with_config(wsr, Some(cfg))
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("error during the clone handshake"))
.split()
.unwrap();
.expect("ooops");
}
Ok(())
}
async fn connection_for_sending(
ws: TcpStream,
mut receiver: SplitStream<WebSocketStream<TcpStream>>,
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 {
@ -127,10 +92,6 @@ async fn connection_for_sending(
"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 {
println!("seems, that messsage formatted wrong");
println!("{:?}", res);
@ -143,6 +104,5 @@ async fn connection_for_sending(
}
}
}
Ok(())
}