From 2c399e2d9285361469628e214790ec07a0599fe3 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 4 May 2024 17:30:02 +0800 Subject: [PATCH] 1 --- server/stat/src/main.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/server/stat/src/main.rs b/server/stat/src/main.rs index 45601cf..9b79ade 100755 --- a/server/stat/src/main.rs +++ b/server/stat/src/main.rs @@ -6,39 +6,44 @@ mod upstream; mod downstream; mod ss; use crate::app::UserApp; -use actix_web::{web, App as ActixApp, get, post, Responder, HttpServer, HttpResponse}; +use actix_web::{web, App as ActixApp, get, post, Responder, Error, HttpServer, HttpRequest, HttpResponse}; use std::sync::mpsc; use std::thread; use actix_rt::System; use std::time::Duration; +use actix_web_actors::ws; +use actix::{Actor, StreamHandler}; -async fn index() -> impl Responder { - "Hello world34444!" -} -#[get("/")] -async fn hello() -> impl Responder { - HttpResponse::Ok().body("Hello world!") +/// Define HTTP actor +struct MyWs; + +impl Actor for MyWs { + type Context = ws::WebsocketContext; } -#[post("/echo")] -async fn echo(req_body: String) -> impl Responder { - HttpResponse::Ok().body(req_body) +/// Handler for ws::Message message +impl StreamHandler> for MyWs { + fn handle(&mut self, msg: Result, ctx: &mut Self::Context) { + match msg { + Ok(ws::Message::Ping(msg)) => ctx.pong(&msg), + Ok(ws::Message::Text(text)) => ctx.text(text), + Ok(ws::Message::Binary(bin)) => ctx.binary(bin), + _ => (), + } + } } -async fn manual_hello() -> impl Responder { - HttpResponse::Ok().body("Hey there!") +async fn index(req: HttpRequest, stream: web::Payload) -> Result { + let resp = ws::start(MyWs {}, &req, stream); + println!("{:?}", resp); + resp } fn main() { let t1 = thread::spawn(||{ let mut rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { - HttpServer::new(|| { - ActixApp::new() - .service(hello) - .service(echo) - .route("/hey", web::get().to(manual_hello)) - }) + HttpServer::new(|| ActixApp::new().route("/ws/", web::get().to(index))) .bind(("127.0.0.1", 8080))? .run() .await