mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
no need for StreamHandler
This commit is contained in:
parent
3ed9e872ad
commit
247c23c1ea
@ -77,7 +77,7 @@ tokio-tls = { version="0.1", optional = true }
|
||||
tokio-openssl = { version="0.1", optional = true }
|
||||
|
||||
[dependencies.actix]
|
||||
version = "0.4"
|
||||
version = "^0.4.1"
|
||||
|
||||
[dependencies.openssl]
|
||||
version = "0.9"
|
||||
|
@ -36,7 +36,6 @@ impl Actor for MyWebSocket {
|
||||
type Context = HttpContext<Self, AppState>;
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message> for MyWebSocket {}
|
||||
impl Handler<ws::Message> for MyWebSocket {
|
||||
type Result = ();
|
||||
|
||||
|
@ -53,6 +53,32 @@ struct WsChatSession {
|
||||
|
||||
impl Actor for WsChatSession {
|
||||
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
|
||||
@ -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() {
|
||||
let _ = env_logger::init();
|
||||
let sys = actix::System::new("websocket-example");
|
||||
|
@ -185,8 +185,6 @@ impl Actor for TcpServer {
|
||||
struct TcpConnect(TcpStream, net::SocketAddr);
|
||||
|
||||
/// Handle stream of TcpStream's
|
||||
impl StreamHandler<TcpConnect> for TcpServer {}
|
||||
|
||||
impl Handler<TcpConnect> for TcpServer {
|
||||
type Result = ();
|
||||
|
||||
|
@ -24,17 +24,7 @@ impl Actor for MyWebSocket {
|
||||
type Context = HttpContext<Self>;
|
||||
}
|
||||
|
||||
/// 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");
|
||||
}
|
||||
}
|
||||
|
||||
/// Handler for `ws::Message`
|
||||
impl Handler<ws::Message> for MyWebSocket {
|
||||
type Result = ();
|
||||
|
||||
|
@ -21,10 +21,9 @@ impl Actor for Ws {
|
||||
}
|
||||
|
||||
/// Define Handler for ws::Message message
|
||||
# impl StreamHandler<ws::Message> for Ws {}
|
||||
impl Handler<ws::Message> for Ws {
|
||||
type Result=();
|
||||
|
||||
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) {
|
||||
match msg {
|
||||
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg),
|
||||
|
@ -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>
|
||||
where H: HttpHandler + 'static,
|
||||
{
|
||||
|
@ -23,8 +23,6 @@
|
||||
//! }
|
||||
//!
|
||||
//! // Define Handler for ws::Message message
|
||||
//! # impl StreamHandler<ws::Message> for Ws {}
|
||||
//! #
|
||||
//! impl Handler<ws::Message> for Ws {
|
||||
//! type Result = ();
|
||||
//!
|
||||
@ -49,7 +47,7 @@ use http::{Method, StatusCode, header};
|
||||
use bytes::BytesMut;
|
||||
use futures::{Async, Poll, Stream};
|
||||
|
||||
use actix::{Actor, AsyncContext, ResponseType, StreamHandler};
|
||||
use actix::{Actor, AsyncContext, ResponseType, Handler};
|
||||
|
||||
use payload::ReadAny;
|
||||
use error::{Error, WsHandshakeError};
|
||||
@ -86,7 +84,7 @@ impl ResponseType for Message {
|
||||
|
||||
/// Do websocket handshake and start actor
|
||||
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
|
||||
{
|
||||
let mut resp = handshake(&req)?;
|
||||
|
Loading…
Reference in New Issue
Block a user