mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-04 05:17:39 +03:00
refactor http actor handling
This commit is contained in:
parent
6177d86d97
commit
6f833798c7
11 changed files with 131 additions and 211 deletions
31
README.md
31
README.md
|
@ -80,35 +80,10 @@ impl Actor for MyWebSocket {
|
|||
type Context = HttpContext<Self>;
|
||||
}
|
||||
|
||||
/// Http route handler
|
||||
impl Route for MyWebSocket {
|
||||
type State = ();
|
||||
|
||||
fn request(mut req: HttpRequest, mut ctx: HttpContext<Self>) -> Result<Reply>
|
||||
{
|
||||
// websocket handshake
|
||||
let resp = ws::handshake(&req)?;
|
||||
// send HttpResponse back to peer
|
||||
ctx.start(resp);
|
||||
// convert bytes stream to a stream of `ws::Message` and handle stream
|
||||
ctx.add_stream(ws::WsStream::new(&mut req));
|
||||
ctx.reply(MyWebSocket)
|
||||
}
|
||||
}
|
||||
|
||||
/// Standard actix's stream handler for a stream of `ws::Message`
|
||||
impl StreamHandler<ws::Message> for MyWebSocket {
|
||||
fn started(&mut self, ctx: &mut Self::Context) {
|
||||
println!("WebSocket session openned");
|
||||
}
|
||||
|
||||
fn finished(&mut self, ctx: &mut Self::Context) {
|
||||
println!("WebSocket session closed");
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message> for MyWebSocket {}
|
||||
impl Handler<ws::Message> for MyWebSocket {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context)
|
||||
-> Response<Self, ws::Message>
|
||||
{
|
||||
// process websocket messages
|
||||
|
@ -136,7 +111,7 @@ fn main() {
|
|||
// enable logger
|
||||
.middleware(middlewares::Logger::default())
|
||||
// websocket route
|
||||
.resource("/ws/", |r| r.get::<MyWebSocket>())
|
||||
.resource("/ws/", |r| r.get(|req| ws::start(req, MyWebSocket)))
|
||||
.route_handler("/", StaticFiles::new("examples/static/", true)))
|
||||
.serve::<_, ()>("127.0.0.1:8080").unwrap();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue