mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
update actix; update examples
This commit is contained in:
@ -18,8 +18,13 @@ impl Route for MyRoute {
|
||||
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self> {
|
||||
println!("{:?}", req);
|
||||
|
||||
let multipart = match req.multipart(payload) {
|
||||
Ok(mp) => mp,
|
||||
Err(e) => return Reply::reply(e),
|
||||
};
|
||||
|
||||
// get Multipart stream
|
||||
WrapStream::<MyRoute>::actstream(req.multipart(payload)?)
|
||||
WrapStream::<MyRoute>::actstream(multipart)
|
||||
.and_then(|item, act, ctx| {
|
||||
// Multipart stream is a stream of Fields and nested Multiparts
|
||||
match item {
|
||||
|
@ -58,6 +58,11 @@ struct ChatClient;
|
||||
|
||||
struct ClientCommand(String);
|
||||
|
||||
impl ResponseType for ClientCommand {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl Actor for ChatClient {
|
||||
type Context = FramedContext<Self>;
|
||||
|
||||
@ -112,11 +117,6 @@ impl Handler<ClientCommand> for ChatClient
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ClientCommand> for ChatClient {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Server communication
|
||||
|
||||
impl FramedActor for ChatClient {
|
||||
@ -134,11 +134,6 @@ impl StreamHandler<codec::ChatResponse, io::Error> for ChatClient {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<codec::ChatResponse> for ChatClient {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl Handler<codec::ChatResponse, io::Error> for ChatClient {
|
||||
|
||||
fn handle(&mut self, msg: codec::ChatResponse, _: &mut FramedContext<Self>)
|
||||
|
@ -4,7 +4,7 @@ use serde_json as json;
|
||||
use byteorder::{BigEndian , ByteOrder};
|
||||
use bytes::{BytesMut, BufMut};
|
||||
use tokio_io::codec::{Encoder, Decoder};
|
||||
|
||||
use actix::ResponseType;
|
||||
|
||||
/// Client request
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@ -20,6 +20,11 @@ pub enum ChatRequest {
|
||||
Ping
|
||||
}
|
||||
|
||||
impl ResponseType for ChatRequest {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Server response
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(tag="cmd", content="data")]
|
||||
@ -36,6 +41,10 @@ pub enum ChatResponse {
|
||||
Message(String),
|
||||
}
|
||||
|
||||
impl ResponseType for ChatResponse {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Codec for Client -> Server transport
|
||||
pub struct ChatCodec;
|
||||
|
@ -2,6 +2,7 @@
|
||||
extern crate rand;
|
||||
extern crate bytes;
|
||||
extern crate byteorder;
|
||||
extern crate futures;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_core;
|
||||
extern crate env_logger;
|
||||
@ -78,11 +79,6 @@ impl Handler<session::Message> for WsChatSession {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<session::Message> for WsChatSession {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// WebSocket message handler
|
||||
impl Handler<ws::Message> for WsChatSession {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
@ -194,11 +190,6 @@ impl StreamHandler<ws::Message> for WsChatSession
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ws::Message> for WsChatSession {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let _ = env_logger::init();
|
||||
|
@ -16,11 +16,24 @@ pub struct Connect {
|
||||
pub addr: Box<Subscriber<session::Message> + Send>,
|
||||
}
|
||||
|
||||
/// Response type for Connect message
|
||||
///
|
||||
/// Chat server returns unique session id
|
||||
impl ResponseType for Connect {
|
||||
type Item = usize;
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Session is disconnected
|
||||
pub struct Disconnect {
|
||||
pub id: usize,
|
||||
}
|
||||
|
||||
impl ResponseType for Disconnect {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Send message to specific room
|
||||
pub struct Message {
|
||||
/// Id of the client session
|
||||
@ -31,9 +44,19 @@ pub struct Message {
|
||||
pub room: String,
|
||||
}
|
||||
|
||||
impl ResponseType for Message {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// List of available rooms
|
||||
pub struct ListRooms;
|
||||
|
||||
impl ResponseType for ListRooms {
|
||||
type Item = Vec<String>;
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Join room, if room does not exists create new one.
|
||||
pub struct Join {
|
||||
/// Client id
|
||||
@ -42,6 +65,11 @@ pub struct Join {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl ResponseType for Join {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// `ChatServer` manages chat rooms and responsible for coordinating chat session.
|
||||
/// implementation is super primitive
|
||||
pub struct ChatServer {
|
||||
@ -109,15 +137,6 @@ impl Handler<Connect> for ChatServer {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<Connect> for ChatServer {
|
||||
/// Response type for Connect message
|
||||
///
|
||||
/// Chat server returns unique session id
|
||||
type Item = usize;
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
|
||||
/// Handler for Disconnect message.
|
||||
impl Handler<Disconnect> for ChatServer {
|
||||
|
||||
@ -144,11 +163,6 @@ impl Handler<Disconnect> for ChatServer {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<Disconnect> for ChatServer {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Handler for Message message.
|
||||
impl Handler<Message> for ChatServer {
|
||||
|
||||
@ -159,11 +173,6 @@ impl Handler<Message> for ChatServer {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<Message> for ChatServer {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Handler for `ListRooms` message.
|
||||
impl Handler<ListRooms> for ChatServer {
|
||||
|
||||
@ -178,11 +187,6 @@ impl Handler<ListRooms> for ChatServer {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ListRooms> for ChatServer {
|
||||
type Item = Vec<String>;
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// Join room, send disconnect message to old room
|
||||
/// send join message to new room
|
||||
impl Handler<Join> for ChatServer {
|
||||
@ -211,8 +215,3 @@ impl Handler<Join> for ChatServer {
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<Join> for ChatServer {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
use std::{io, net};
|
||||
use std::str::FromStr;
|
||||
use std::time::{Instant, Duration};
|
||||
use futures::Stream;
|
||||
use tokio_core::net::{TcpStream, TcpListener};
|
||||
|
||||
use actix::*;
|
||||
@ -14,6 +15,10 @@ use codec::{ChatRequest, ChatResponse, ChatCodec};
|
||||
/// Chat server sends this messages to session
|
||||
pub struct Message(pub String);
|
||||
|
||||
impl ResponseType for Message {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
/// `ChatSession` actor is responsible for tcp peer communitions.
|
||||
pub struct ChatSession {
|
||||
@ -68,11 +73,6 @@ impl StreamHandler<ChatRequest, io::Error> for ChatSession {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ChatRequest> for ChatSession {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl Handler<ChatRequest, io::Error> for ChatSession {
|
||||
|
||||
/// We'll stop chat session actor on any error, high likely it is just
|
||||
@ -137,12 +137,6 @@ impl Handler<Message> for ChatSession {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<Message> for ChatSession {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
|
||||
/// Helper methods
|
||||
impl ChatSession {
|
||||
|
||||
@ -194,7 +188,7 @@ impl TcpServer {
|
||||
// So to be able to handle this events `Server` actor has to implement
|
||||
// stream handler `StreamHandler<(TcpStream, net::SocketAddr), io::Error>`
|
||||
let _: () = TcpServer::create(|ctx| {
|
||||
ctx.add_stream(listener.incoming());
|
||||
ctx.add_stream(listener.incoming().map(|(t, a)| TcpConnect(t, a)));
|
||||
TcpServer{chat: chat}
|
||||
});
|
||||
}
|
||||
@ -206,18 +200,19 @@ impl Actor for TcpServer {
|
||||
type Context = Context<Self>;
|
||||
}
|
||||
|
||||
/// Handle stream of TcpStream's
|
||||
impl StreamHandler<(TcpStream, net::SocketAddr), io::Error> for TcpServer {}
|
||||
struct TcpConnect(TcpStream, net::SocketAddr);
|
||||
|
||||
impl ResponseType<(TcpStream, net::SocketAddr)> for TcpServer {
|
||||
impl ResponseType for TcpConnect {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl Handler<(TcpStream, net::SocketAddr), io::Error> for TcpServer {
|
||||
/// Handle stream of TcpStream's
|
||||
impl StreamHandler<TcpConnect, io::Error> for TcpServer {}
|
||||
|
||||
fn handle(&mut self, msg: (TcpStream, net::SocketAddr), _: &mut Context<Self>)
|
||||
-> Response<Self, (TcpStream, net::SocketAddr)>
|
||||
impl Handler<TcpConnect, io::Error> for TcpServer {
|
||||
|
||||
fn handle(&mut self, msg: TcpConnect, _: &mut Context<Self>) -> Response<Self, TcpConnect>
|
||||
{
|
||||
// For each incoming connection we create `ChatSession` actor
|
||||
// with out chat server address.
|
||||
|
@ -30,12 +30,15 @@ impl Route for MyWebSocket {
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ws::Message> for MyWebSocket {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
impl StreamHandler<ws::Message> for MyWebSocket {
|
||||
fn started(&mut self, ctx: &mut Self::Context) {
|
||||
println!("WebSocket session openned");
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message> for MyWebSocket {}
|
||||
fn finished(&mut self, ctx: &mut Self::Context) {
|
||||
println!("WebSocket session closed");
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<ws::Message> for MyWebSocket {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
|
Reference in New Issue
Block a user