1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

upgrade example to actix-web 0.7

This commit is contained in:
Nikolay Kim
2018-07-16 12:36:53 +06:00
parent 0b52c7850e
commit 2cc1b23761
57 changed files with 913 additions and 693 deletions

View File

@ -20,5 +20,6 @@ env_logger = "*"
serde = "1.0"
serde_json = "1.0"
actix = "0.5"
actix-web = "^0.6"
actix = "0.7"
#actix-web = "^0.7"
actix-web = { git = "https://github.com/actix/actix-web.git" }

View File

@ -24,11 +24,11 @@ mod server;
/// This is our websocket route state, this state is shared with all route
/// instances via `HttpContext::state()`
struct WsChatSessionState {
addr: Addr<Syn, server::ChatServer>,
addr: Addr<server::ChatServer>,
}
/// Entry point for our route
fn chat_route(req: HttpRequest<WsChatSessionState>) -> Result<HttpResponse, Error> {
fn chat_route(req: &HttpRequest<WsChatSessionState>) -> Result<HttpResponse, Error> {
ws::start(
req,
WsChatSession {
@ -63,7 +63,7 @@ impl Actor for WsChatSession {
// before processing any other events.
// HttpContext::state() is instance of WsChatSessionState, state is shared
// across all routes within application
let addr: Addr<Syn, _> = ctx.address();
let addr = ctx.address();
ctx.state()
.addr
.send(server::Connect {
@ -183,7 +183,7 @@ fn main() {
let sys = actix::System::new("websocket-example");
// Start chat server actor in separate thread
let server: Addr<Syn, _> = Arbiter::start(|_| server::ChatServer::default());
let server = Arbiter::start(|_| server::ChatServer::default());
// Create Http server with websocket support
HttpServer::new(move || {
@ -193,16 +193,16 @@ fn main() {
};
App::with_state(state)
// redirect to websocket.html
.resource("/", |r| r.method(http::Method::GET).f(|_| {
HttpResponse::Found()
.header("LOCATION", "/static/websocket.html")
.finish()
}))
// websocket
.resource("/ws/", |r| r.route().f(chat_route))
// static resources
.handler("/static/", fs::StaticFiles::new("static/"))
// redirect to websocket.html
.resource("/", |r| r.method(http::Method::GET).f(|_| {
HttpResponse::Found()
.header("LOCATION", "/static/websocket.html")
.finish()
}))
// websocket
.resource("/ws/", |r| r.route().f(chat_route))
// static resources
.handler("/static/", fs::StaticFiles::new("static/").unwrap())
}).bind("127.0.0.1:8080")
.unwrap()
.start();

View File

@ -17,7 +17,7 @@ pub struct Message(pub String);
#[derive(Message)]
#[rtype(usize)]
pub struct Connect {
pub addr: Recipient<Syn, Message>,
pub addr: Recipient<Message>,
}
/// Session is disconnected
@ -56,7 +56,7 @@ pub struct Join {
/// `ChatServer` manages chat rooms and responsible for coordinating chat
/// session. implementation is super primitive
pub struct ChatServer {
sessions: HashMap<usize, Recipient<Syn, Message>>,
sessions: HashMap<usize, Recipient<Message>>,
rooms: HashMap<String, HashSet<usize>>,
rng: RefCell<ThreadRng>,
}