mirror of
https://github.com/actix/actix-web.git
synced 2024-12-24 09:00:34 +00:00
no need for StreamHandler
This commit is contained in:
parent
3ed9e872ad
commit
247c23c1ea
8 changed files with 31 additions and 55 deletions
|
@ -77,7 +77,7 @@ tokio-tls = { version="0.1", optional = true }
|
||||||
tokio-openssl = { version="0.1", optional = true }
|
tokio-openssl = { version="0.1", optional = true }
|
||||||
|
|
||||||
[dependencies.actix]
|
[dependencies.actix]
|
||||||
version = "0.4"
|
version = "^0.4.1"
|
||||||
|
|
||||||
[dependencies.openssl]
|
[dependencies.openssl]
|
||||||
version = "0.9"
|
version = "0.9"
|
||||||
|
|
|
@ -36,7 +36,6 @@ impl Actor for MyWebSocket {
|
||||||
type Context = HttpContext<Self, AppState>;
|
type Context = HttpContext<Self, AppState>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StreamHandler<ws::Message> for MyWebSocket {}
|
|
||||||
impl Handler<ws::Message> for MyWebSocket {
|
impl Handler<ws::Message> for MyWebSocket {
|
||||||
type Result = ();
|
type Result = ();
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,32 @@ struct WsChatSession {
|
||||||
|
|
||||||
impl Actor for WsChatSession {
|
impl Actor for WsChatSession {
|
||||||
type Context = HttpContext<Self, WsChatSessionState>;
|
type Context = HttpContext<Self, WsChatSessionState>;
|
||||||
|
|
||||||
|
/// Method is called on actor start.
|
||||||
|
/// We register ws session with ChatServer
|
||||||
|
fn started(&mut self, ctx: &mut Self::Context) {
|
||||||
|
// register self in chat server. `AsyncContext::wait` register
|
||||||
|
// future within context, but context waits until this future resolves
|
||||||
|
// before processing any other events.
|
||||||
|
// HttpContext::state() is instance of WsChatSessionState, state is shared across all
|
||||||
|
// routes within application
|
||||||
|
let subs = ctx.sync_subscriber();
|
||||||
|
ctx.state().addr.call(
|
||||||
|
self, server::Connect{addr: subs}).then(
|
||||||
|
|res, act, ctx| {
|
||||||
|
match res {
|
||||||
|
Ok(Ok(res)) => act.id = res,
|
||||||
|
// something is wrong with chat server
|
||||||
|
_ => ctx.stop(),
|
||||||
|
}
|
||||||
|
fut::ok(())
|
||||||
|
}).wait(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stopping(&mut self, ctx: &mut Self::Context) {
|
||||||
|
// notify chat server
|
||||||
|
ctx.state().addr.send(server::Disconnect{id: self.id});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle messages from chat server, we simply send it to peer websocket
|
/// Handle messages from chat server, we simply send it to peer websocket
|
||||||
|
@ -143,37 +169,6 @@ impl Handler<ws::Message> for WsChatSession {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StreamHandler<ws::Message> for WsChatSession
|
|
||||||
{
|
|
||||||
/// Method is called when stream get polled first time.
|
|
||||||
/// We register ws session with ChatServer
|
|
||||||
fn started(&mut self, ctx: &mut Self::Context) {
|
|
||||||
// register self in chat server. `AsyncContext::wait` register
|
|
||||||
// future within context, but context waits until this future resolves
|
|
||||||
// before processing any other events.
|
|
||||||
// HttpContext::state() is instance of WsChatSessionState, state is shared across all
|
|
||||||
// routes within application
|
|
||||||
let subs = ctx.sync_subscriber();
|
|
||||||
ctx.state().addr.call(
|
|
||||||
self, server::Connect{addr: subs}).then(
|
|
||||||
|res, act, ctx| {
|
|
||||||
match res {
|
|
||||||
Ok(Ok(res)) => act.id = res,
|
|
||||||
// something is wrong with chat server
|
|
||||||
_ => ctx.stop(),
|
|
||||||
}
|
|
||||||
fut::ok(())
|
|
||||||
}).wait(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Method is called when stream finishes, even if stream finishes with error.
|
|
||||||
fn finished(&mut self, ctx: &mut Self::Context) {
|
|
||||||
// notify chat server
|
|
||||||
ctx.state().addr.send(server::Disconnect{id: self.id});
|
|
||||||
ctx.stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
let sys = actix::System::new("websocket-example");
|
let sys = actix::System::new("websocket-example");
|
||||||
|
|
|
@ -185,8 +185,6 @@ impl Actor for TcpServer {
|
||||||
struct TcpConnect(TcpStream, net::SocketAddr);
|
struct TcpConnect(TcpStream, net::SocketAddr);
|
||||||
|
|
||||||
/// Handle stream of TcpStream's
|
/// Handle stream of TcpStream's
|
||||||
impl StreamHandler<TcpConnect> for TcpServer {}
|
|
||||||
|
|
||||||
impl Handler<TcpConnect> for TcpServer {
|
impl Handler<TcpConnect> for TcpServer {
|
||||||
type Result = ();
|
type Result = ();
|
||||||
|
|
||||||
|
|
|
@ -24,17 +24,7 @@ impl Actor for MyWebSocket {
|
||||||
type Context = HttpContext<Self>;
|
type Context = HttpContext<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Standard actix's stream handler for a stream of `ws::Message`
|
/// Handler for `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 Handler<ws::Message> for MyWebSocket {
|
impl Handler<ws::Message> for MyWebSocket {
|
||||||
type Result = ();
|
type Result = ();
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ impl Actor for Ws {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Define Handler for ws::Message message
|
/// Define Handler for ws::Message message
|
||||||
# impl StreamHandler<ws::Message> for Ws {}
|
|
||||||
impl Handler<ws::Message> for Ws {
|
impl Handler<ws::Message> for Ws {
|
||||||
type Result=();
|
type Result=();
|
||||||
|
|
||||||
|
|
|
@ -151,9 +151,6 @@ impl<H: 'static> Actor for Worker<H> where H: HttpHandler + 'static {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H> StreamHandler<Conn<net::TcpStream>> for Worker<H>
|
|
||||||
where H: HttpHandler + 'static {}
|
|
||||||
|
|
||||||
impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
|
impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
|
||||||
where H: HttpHandler + 'static,
|
where H: HttpHandler + 'static,
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,8 +23,6 @@
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! // Define Handler for ws::Message message
|
//! // Define Handler for ws::Message message
|
||||||
//! # impl StreamHandler<ws::Message> for Ws {}
|
|
||||||
//! #
|
|
||||||
//! impl Handler<ws::Message> for Ws {
|
//! impl Handler<ws::Message> for Ws {
|
||||||
//! type Result = ();
|
//! type Result = ();
|
||||||
//!
|
//!
|
||||||
|
@ -49,7 +47,7 @@ use http::{Method, StatusCode, header};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{Async, Poll, Stream};
|
use futures::{Async, Poll, Stream};
|
||||||
|
|
||||||
use actix::{Actor, AsyncContext, ResponseType, StreamHandler};
|
use actix::{Actor, AsyncContext, ResponseType, Handler};
|
||||||
|
|
||||||
use payload::ReadAny;
|
use payload::ReadAny;
|
||||||
use error::{Error, WsHandshakeError};
|
use error::{Error, WsHandshakeError};
|
||||||
|
@ -86,7 +84,7 @@ impl ResponseType for Message {
|
||||||
|
|
||||||
/// Do websocket handshake and start actor
|
/// Do websocket handshake and start actor
|
||||||
pub fn start<A, S>(mut req: HttpRequest<S>, actor: A) -> Result<HttpResponse, Error>
|
pub fn start<A, S>(mut req: HttpRequest<S>, actor: A) -> Result<HttpResponse, Error>
|
||||||
where A: Actor<Context=HttpContext<A, S>> + StreamHandler<Message>,
|
where A: Actor<Context=HttpContext<A, S>> + Handler<Message>,
|
||||||
S: 'static
|
S: 'static
|
||||||
{
|
{
|
||||||
let mut resp = handshake(&req)?;
|
let mut resp = handshake(&req)?;
|
||||||
|
|
Loading…
Reference in a new issue