diff --git a/examples/tls/src/main.rs b/examples/tls/src/main.rs index 0be922aa..81574e5e 100644 --- a/examples/tls/src/main.rs +++ b/examples/tls/src/main.rs @@ -9,12 +9,12 @@ use std::io::Read; use actix_web::*; /// somple handle -fn index(req: HttpRequest) -> HttpResponse { +fn index(req: HttpRequest) -> Result { println!("{:?}", req); - httpcodes::HTTPOk - .build() - .content_type("text/plain") - .body("Welcome!").unwrap() + Ok(httpcodes::HTTPOk + .build() + .content_type("text/plain") + .body("Welcome!")?) } fn main() { @@ -37,10 +37,10 @@ fn main() { .handler("/index.html", index) // with path parameters .resource("/", |r| r.handler(Method::GET, |req| { - Ok(httpcodes::HTTPFound - .build() - .header("LOCATION", "/index.html") - .body(Body::Empty)?) + httpcodes::HTTPFound + .build() + .header("LOCATION", "/index.html") + .body(Body::Empty) }))) .serve_tls::<_, ()>("127.0.0.1:8080", pkcs12).unwrap(); diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index d7176f56..ae123d5e 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -22,13 +22,23 @@ mod codec; mod server; mod session; - /// This is our websocket route state, this state is shared with all route instances /// via `HttpContext::state()` struct WsChatSessionState { addr: SyncAddress, } +/// Entry point for our route +fn chat_route(req: HttpRequest) -> Result { + ws::start( + req, + WsChatSession { + id: 0, + hb: Instant::now(), + room: "Main".to_owned(), + name: None}) +} + struct WsChatSession { /// unique session id id: usize, @@ -41,32 +51,12 @@ struct WsChatSession { } impl Actor for WsChatSession { - type Context = HttpContext; -} - -/// Entry point for our route -impl Route for WsChatSession { - type State = WsChatSessionState; - - fn request(mut req: HttpRequest, - ctx: &mut HttpContext) -> RouteResult - { - // websocket handshakre, it may fail if request is not websocket request - let resp = ws::handshake(&req)?; - ctx.start(resp); - ctx.add_stream(ws::WsStream::new(&mut req)); - Reply::async( - WsChatSession { - id: 0, - hb: Instant::now(), - room: "Main".to_owned(), - name: None}) - } + type Context = HttpContext; } /// Handle messages from chat server, we simply send it to peer websocket impl Handler for WsChatSession { - fn handle(&mut self, msg: session::Message, ctx: &mut HttpContext) + fn handle(&mut self, msg: session::Message, ctx: &mut Self::Context) -> Response { ws::WsWriter::text(ctx, &msg.0); @@ -76,7 +66,7 @@ impl Handler for WsChatSession { /// WebSocket message handler impl Handler for WsChatSession { - fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext) + fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) -> Response { println!("WEBSOCKET MESSAGE: {:?}", msg); @@ -209,17 +199,16 @@ fn main() { HttpServer::new( Application::build("/", state) // redirect to websocket.html - .resource("/", |r| - r.handler(Method::GET, |req| { - Ok(httpcodes::HTTPFound - .build() - .header("LOCATION", "/static/websocket.html") - .body(Body::Empty)?) - })) + .resource("/", |r| r.handler(Method::GET, |req| { + httpcodes::HTTPFound + .build() + .header("LOCATION", "/static/websocket.html") + .body(Body::Empty) + })) // websocket - .resource("/ws/", |r| r.get::()) + .resource("/ws/", |r| r.get(chat_route)) // static resources - .route_handler("/static", StaticFiles::new("static/", true))) + .route("/static", StaticFiles::new("static/", true))) .serve::<_, ()>("127.0.0.1:8080").unwrap(); let _ = sys.run();