simple websocket
This commit is contained in:
parent
160acb72a2
commit
eb3271773a
@ -15,10 +15,17 @@ repository = "https://www.github.com/HsuJv/webgateway"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
actix = "0.10.0"
|
||||||
actix-web = "3.3.2"
|
actix-web = "3.3.2"
|
||||||
actix-files = "0.5.0"
|
actix-files = "0.5.0"
|
||||||
tokio-tar = "0.3.0"
|
tokio-tar = "0.3.0"
|
||||||
urlencoding = "2.1.0"
|
urlencoding = "2.1.0"
|
||||||
|
actix-web-actors = "3.0.0"
|
||||||
|
|
||||||
|
# log systems
|
||||||
|
femme = "1.3"
|
||||||
|
log = "0.4"
|
||||||
|
async-log = "2.0.0"
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
panic = "unwind"
|
panic = "unwind"
|
||||||
|
@ -1,31 +1,46 @@
|
|||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::{ContentEncoding, StatusCode};
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
|
|
||||||
const STATIC_DIR: &str = "./static/";
|
use femme;
|
||||||
// #[get("/{id}/{name}/index.html")]
|
use log::info;
|
||||||
// async fn index(web::Path((id, name)): web::Path<(u32, String)>) -> impl Responder {
|
|
||||||
// format!("Hello {}! id:{}", name, id)
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
mod ws;
|
||||||
|
|
||||||
|
const STATIC_DIR: &str = "./static/";
|
||||||
|
const PAGE_INDEX: &str = "./static/index.html";
|
||||||
|
const PAGE_NOT_FOUND: &str = "./static/p404.html";
|
||||||
|
|
||||||
|
fn setup_logger() {
|
||||||
|
let logger = femme::pretty::Logger::new();
|
||||||
|
async_log::Logger::wrap(logger, || 12)
|
||||||
|
.start(log::LevelFilter::Trace)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
async fn index(_: HttpRequest) -> Result<fs::NamedFile> {
|
async fn index(_: HttpRequest) -> Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open(format!("{}/index.html", STATIC_DIR))?)
|
Ok(fs::NamedFile::open(PAGE_INDEX)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn p404() -> Result<fs::NamedFile> {
|
async fn p404() -> Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open(format!("{}/p404.html", STATIC_DIR))?
|
Ok(fs::NamedFile::open(PAGE_NOT_FOUND)?.set_status_code(StatusCode::NOT_FOUND))
|
||||||
.set_status_code(StatusCode::NOT_FOUND))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
setup_logger();
|
||||||
|
|
||||||
|
info!("Server starts at http://127.0.0.1:8080");
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.service(web::resource("/").route(web::get().to(index)))
|
.wrap(middleware::Compress::new(ContentEncoding::Gzip))
|
||||||
|
.service(index)
|
||||||
|
.service(web::resource("/ws").route(web::get().to(ws::index)))
|
||||||
.service(
|
.service(
|
||||||
fs::Files::new("/static", STATIC_DIR)
|
fs::Files::new("/static", STATIC_DIR)
|
||||||
.prefer_utf8(true)
|
.prefer_utf8(true)
|
||||||
.index_file(format!("{}/index.html", STATIC_DIR))
|
.index_file(PAGE_INDEX)
|
||||||
.use_etag(true)
|
.use_etag(true)
|
||||||
.default_handler(web::route().to(p404)),
|
.default_handler(web::route().to(p404)),
|
||||||
)
|
)
|
||||||
@ -34,5 +49,6 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
32
backend/src/ws.rs
Normal file
32
backend/src/ws.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
use actix::{Actor, StreamHandler};
|
||||||
|
use actix_web::{web, Error, HttpRequest, HttpResponse};
|
||||||
|
use actix_web_actors::ws;
|
||||||
|
use log::*;
|
||||||
|
|
||||||
|
/// Define HTTP actor
|
||||||
|
struct MyWs;
|
||||||
|
|
||||||
|
impl Actor for MyWs {
|
||||||
|
type Context = ws::WebsocketContext<Self>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||||
|
let resp = ws::start(MyWs {}, &req, stream);
|
||||||
|
match &resp {
|
||||||
|
Ok(resp) => info!("{:?}", resp),
|
||||||
|
Err(e) => error!("{:?}", e),
|
||||||
|
}
|
||||||
|
resp
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user