Initial tiny test server
This commit is contained in:
commit
528d264b02
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
1437
Cargo.lock
generated
Normal file
1437
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
[package]
|
||||||
|
name = "worldwideportal-server"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-web = "4.9.0"
|
||||||
|
actix-ws = "0.3.0"
|
||||||
|
env_logger = "0.11.5"
|
||||||
|
futures-util = "0.3.30"
|
||||||
|
log = "0.4.22"
|
||||||
|
tokio = { version = "1.39.3", features = ["net", "macros", "tokio-macros", "rt-multi-thread"] }
|
1
rustfmt.toml
Normal file
1
rustfmt.toml
Normal file
@ -0,0 +1 @@
|
|||||||
|
edition = "2021"
|
69
src/main.rs
Normal file
69
src/main.rs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
use actix_web::{
|
||||||
|
self, get,
|
||||||
|
middleware::Logger,
|
||||||
|
rt::{self, net::TcpStream},
|
||||||
|
web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
|
||||||
|
};
|
||||||
|
use actix_ws::{AggregatedMessage, CloseCode, Closed};
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
|
#[get("/ws")]
|
||||||
|
async fn ws(req: HttpRequest, body: web::Payload) -> impl Responder {
|
||||||
|
let (response, mut session, stream) = actix_ws::handle(&req, body)?;
|
||||||
|
let mut stream = stream.aggregate_continuations().max_continuation_size(1024);
|
||||||
|
|
||||||
|
let mut tcp_stream: TcpStream = TcpStream::connect("localhost:4000").await?;
|
||||||
|
|
||||||
|
rt::spawn(async move {
|
||||||
|
let mut readbuf: [u8; 1024] = [0; 1024];
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
ws_msg = stream.next() => {
|
||||||
|
match ws_msg {
|
||||||
|
None => break,
|
||||||
|
Some(Err(_e)) => break,
|
||||||
|
Some(Ok(AggregatedMessage::Binary(bin))) => {
|
||||||
|
if tcp_stream.write_all(&bin).await.is_err() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Ok(AggregatedMessage::Ping(msg))) => {
|
||||||
|
if let Err(Closed) = session.pong(&msg).await {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Ok(_)) => {},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
tcp_data_len = tcp_stream.read(&mut readbuf) => {
|
||||||
|
match tcp_data_len {
|
||||||
|
Err(_e) => break,
|
||||||
|
Ok(0) => break,
|
||||||
|
Ok(n) =>
|
||||||
|
if let Err(Closed) = session.binary(readbuf[0..n].to_vec()).await {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = session.close(Some(CloseCode::Normal.into())).await;
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok::<HttpResponse, Error>(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
|
env_logger::init();
|
||||||
|
HttpServer::new(|| {
|
||||||
|
let logger = Logger::default();
|
||||||
|
App::new().wrap(logger).service(ws)
|
||||||
|
})
|
||||||
|
.bind(("127.0.0.1", 8124))?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user