Build basic ws echo server

This commit is contained in:
Joscha 2022-02-08 03:26:46 +01:00
parent 73ffae3898
commit 70bb6f31cf
3 changed files with 649 additions and 9 deletions

View file

@ -1,11 +1,20 @@
use cove_core::{HelloCmd, HelloRpl, Id, Packet, Rpl};
use futures::{future, StreamExt, TryStreamExt};
use tokio::net::{TcpListener, TcpStream};
fn main() {
let packet = Packet::Rpl {
id: 1337,
rpl: Rpl::Hello(HelloRpl::InvalidName {
reason: "abc".to_string(),
}),
};
println!("{}", serde_json::to_string_pretty(&packet).unwrap());
#[tokio::main]
async fn main() {
let listener = TcpListener::bind(("::0", 40080)).await.unwrap();
while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(conn(stream));
}
}
async fn conn(stream: TcpStream) {
println!("Connection from {}", stream.peer_addr().unwrap());
let stream = tokio_tungstenite::accept_async(stream).await.unwrap();
let (write, read) = stream.split();
read.try_filter(|msg| future::ready(msg.is_text() || msg.is_binary()))
.forward(write)
.await
.unwrap();
}