This commit is contained in:
azw 2024-05-04 17:30:02 +08:00
parent 5e8780bbe9
commit 2c399e2d92

View File

@ -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<Self>;
}
#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
/// Handler for ws::Message message
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, 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<HttpResponse, Error> {
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