mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
cleanup and cargo fmt
This commit is contained in:
parent
2d97219195
commit
e4f1833215
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -1687,6 +1687,16 @@ dependencies = [
|
|||||||
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "static_index"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"actix-web 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "string"
|
name = "string"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -15,6 +15,7 @@ members = [
|
|||||||
"r2d2",
|
"r2d2",
|
||||||
"redis-session",
|
"redis-session",
|
||||||
"state",
|
"state",
|
||||||
|
"static_index",
|
||||||
"template_askama",
|
"template_askama",
|
||||||
"template_tera",
|
"template_tera",
|
||||||
"tls",
|
"tls",
|
||||||
|
@ -12,9 +12,8 @@
|
|||||||
// - POSTing json body
|
// - POSTing json body
|
||||||
// 3. chaining futures into a single response used by an asynch endpoint
|
// 3. chaining futures into a single response used by an asynch endpoint
|
||||||
//
|
//
|
||||||
// There are 2 versions in this example, one that uses Boxed Futures and the other that uses Impl
|
// There are 2 versions in this example, one that uses Boxed Futures and the
|
||||||
// Future, available since rustc v1.26.
|
// other that uses Impl Future, available since rustc v1.26.
|
||||||
|
|
||||||
|
|
||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
@ -28,12 +27,14 @@ extern crate env_logger;
|
|||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate validator;
|
extern crate validator;
|
||||||
|
|
||||||
use actix_web::{client, dev::Handler, http::Method, server, App, AsyncResponder, Body,
|
use actix_web::{
|
||||||
Error, HttpMessage, HttpRequest, HttpResponse, Json, Result};
|
client, http::Method, server, App, AsyncResponder, Error, HttpMessage, HttpResponse,
|
||||||
|
Json,
|
||||||
|
};
|
||||||
use futures::{future::ok as fut_ok, Future};
|
use futures::{future::ok as fut_ok, Future};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use validator::{Validate, ValidationError};
|
use validator::Validate;
|
||||||
|
|
||||||
#[derive(Debug, Validate, Deserialize, Serialize)]
|
#[derive(Debug, Validate, Deserialize, Serialize)]
|
||||||
struct SomeData {
|
struct SomeData {
|
||||||
@ -55,7 +56,6 @@ struct HttpBinResponse {
|
|||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// v1 uses Boxed Futures, which were the only option prior to rustc v1.26
|
// v1 uses Boxed Futures, which were the only option prior to rustc v1.26
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@ -96,7 +96,6 @@ fn create_something_v1(
|
|||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
// v2 uses impl Future, available as of rustc v1.26
|
// v2 uses impl Future, available as of rustc v1.26
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
@ -118,37 +117,36 @@ fn step_x_v2(data: SomeData) -> impl Future<Item = SomeData, Error = Error> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_something_v2(some_data: Json<SomeData>)
|
fn create_something_v2(
|
||||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
some_data: Json<SomeData>,
|
||||||
step_x_v2(some_data.into_inner())
|
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
.and_then(|some_data_2| {
|
step_x_v2(some_data.into_inner()).and_then(|some_data_2| {
|
||||||
step_x_v2(some_data_2).and_then(|some_data_3| {
|
step_x_v2(some_data_2).and_then(|some_data_3| {
|
||||||
step_x_v2(some_data_3).and_then(|d|
|
step_x_v2(some_data_3).and_then(|d| {
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("application/json")
|
.content_type("application/json")
|
||||||
.body(serde_json::to_string(&d).unwrap())
|
.body(serde_json::to_string(&d).unwrap())
|
||||||
.into()))
|
.into())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("asyncio_example");
|
let sys = actix::System::new("asyncio_example");
|
||||||
|
|
||||||
server::new(move || {
|
server::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.resource("/something_v1", |r| {
|
.resource("/something_v1", |r| {
|
||||||
r.method(Method::POST).with(create_something_v1)})
|
r.method(Method::POST).with(create_something_v1)
|
||||||
|
})
|
||||||
.resource("/something_v2", |r| {
|
.resource("/something_v2", |r| {
|
||||||
r.method(Method::POST).with_async(create_something_v2)})
|
r.method(Method::POST).with_async(create_something_v2)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
}).bind("127.0.0.1:8088")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
println!("Started http server: 127.0.0.1:8088");
|
println!("Started http server: 127.0.0.1:8088");
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
|
@ -9,8 +9,9 @@ use futures::Stream;
|
|||||||
|
|
||||||
use actix_web::http::{header, Method, StatusCode};
|
use actix_web::http::{header, Method, StatusCode};
|
||||||
use actix_web::middleware::session::{self, RequestSession};
|
use actix_web::middleware::session::{self, RequestSession};
|
||||||
use actix_web::{error, fs, middleware, pred, server, App, Error, HttpRequest,
|
use actix_web::{
|
||||||
HttpResponse, Result};
|
error, fs, middleware, pred, server, App, Error, HttpRequest, HttpResponse, Result,
|
||||||
|
};
|
||||||
use futures::future::{result, FutureResult};
|
use futures::future::{result, FutureResult};
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
@ -66,10 +67,7 @@ fn with_param(req: HttpRequest) -> HttpResponse {
|
|||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("test/plain")
|
.content_type("test/plain")
|
||||||
.body(format!(
|
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
|
||||||
"Hello {}!",
|
|
||||||
req.match_info().get("name").unwrap()
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use cookie::{Cookie, CookieJar, Key};
|
use cookie::{Cookie, CookieJar, Key};
|
||||||
@ -88,7 +89,8 @@ impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
|
|||||||
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
|
||||||
let mut req = req.clone();
|
let mut req = req.clone();
|
||||||
|
|
||||||
let fut = self.backend
|
let fut = self
|
||||||
|
.backend
|
||||||
.from_request(&mut req)
|
.from_request(&mut req)
|
||||||
.then(move |res| match res {
|
.then(move |res| match res {
|
||||||
Ok(id) => {
|
Ok(id) => {
|
||||||
|
@ -5,7 +5,6 @@ extern crate env_logger;
|
|||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate time;
|
extern crate time;
|
||||||
|
|
||||||
use actix_web::middleware::session::RequestSession;
|
|
||||||
use actix_web::{middleware, server, App, HttpRequest, HttpResponse};
|
use actix_web::{middleware, server, App, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
|
@ -18,8 +18,10 @@ extern crate r2d2;
|
|||||||
extern crate uuid;
|
extern crate uuid;
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::{http, middleware, server, App, AsyncResponder, FutureResponse,
|
use actix_web::{
|
||||||
HttpResponse, Path, State};
|
http, middleware, server, App, AsyncResponder, FutureResponse, HttpResponse, Path,
|
||||||
|
State,
|
||||||
|
};
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::r2d2::{ConnectionManager, Pool};
|
use diesel::r2d2::{ConnectionManager, Pool};
|
||||||
|
@ -3,8 +3,10 @@ extern crate actix_web;
|
|||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
|
||||||
use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error,
|
use actix_web::{
|
||||||
HttpMessage, HttpRequest, HttpResponse};
|
client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage,
|
||||||
|
HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
|
|
||||||
/// Stream client request response and then send body to a server response
|
/// Stream client request response and then send body to a server response
|
||||||
|
@ -9,8 +9,10 @@ extern crate serde_derive;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate json;
|
extern crate json;
|
||||||
|
|
||||||
use actix_web::{error, http, middleware, server, App, AsyncResponder, Error,
|
use actix_web::{
|
||||||
HttpMessage, HttpRequest, HttpResponse, Json};
|
error, http, middleware, server, App, AsyncResponder, Error, HttpMessage,
|
||||||
|
HttpRequest, HttpResponse, Json,
|
||||||
|
};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
|
@ -13,8 +13,10 @@ extern crate env_logger;
|
|||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::{http, middleware, server, App, AsyncResponder, Error, FutureResponse,
|
use actix_web::{
|
||||||
HttpRequest, HttpResponse, Json, State};
|
http, middleware, server, App, AsyncResponder, Error, FutureResponse, HttpRequest,
|
||||||
|
HttpResponse, Json, State,
|
||||||
|
};
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
use juniper::http::graphiql::graphiql_source;
|
use juniper::http::graphiql::graphiql_source;
|
||||||
use juniper::http::GraphQLRequest;
|
use juniper::http::GraphQLRequest;
|
||||||
|
@ -5,8 +5,10 @@ extern crate env_logger;
|
|||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
|
||||||
use actix::*;
|
use actix::*;
|
||||||
use actix_web::{http, middleware, multipart, server, App, AsyncResponder, Error,
|
use actix_web::{
|
||||||
HttpMessage, HttpRequest, HttpResponse};
|
http, middleware, multipart, server, App, AsyncResponder, Error, HttpMessage,
|
||||||
|
HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
use futures::future::{result, Either};
|
use futures::future::{result, Either};
|
||||||
use futures::{Future, Stream};
|
use futures::{Future, Stream};
|
||||||
|
@ -9,8 +9,9 @@ extern crate prost;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate prost_derive;
|
extern crate prost_derive;
|
||||||
|
|
||||||
use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest,
|
use actix_web::{
|
||||||
HttpResponse};
|
http, middleware, server, App, AsyncResponder, Error, HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
mod protobuf;
|
mod protobuf;
|
||||||
|
@ -132,7 +132,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
let limit = self.limit;
|
let limit = self.limit;
|
||||||
let fut = req.from_err()
|
let fut = req
|
||||||
|
.from_err()
|
||||||
.fold(BytesMut::new(), move |mut body, chunk| {
|
.fold(BytesMut::new(), move |mut body, chunk| {
|
||||||
if (body.len() + chunk.len()) > limit {
|
if (body.len() + chunk.len()) > limit {
|
||||||
Err(ProtoBufPayloadError::Overflow)
|
Err(ProtoBufPayloadError::Overflow)
|
||||||
|
@ -35,10 +35,10 @@ impl Handler<CreateUser> for DbExecutor {
|
|||||||
&[&uuid, &msg.name],
|
&[&uuid, &msg.name],
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
Ok(conn.query_row(
|
Ok(conn
|
||||||
"SELECT name FROM users WHERE id=$1",
|
.query_row("SELECT name FROM users WHERE id=$1", &[&uuid], |row| {
|
||||||
&[&uuid],
|
row.get(0)
|
||||||
|row| row.get(0),
|
})
|
||||||
).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?)
|
.map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,9 @@ extern crate serde_json;
|
|||||||
extern crate uuid;
|
extern crate uuid;
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest,
|
use actix_web::{
|
||||||
HttpResponse};
|
http, middleware, server, App, AsyncResponder, Error, HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
use r2d2_sqlite::SqliteConnectionManager;
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
|
|
||||||
|
@ -29,10 +29,7 @@ fn index(req: HttpRequest<AppState>) -> HttpResponse {
|
|||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
req.state().counter.set(req.state().counter.get() + 1);
|
req.state().counter.set(req.state().counter.get() + 1);
|
||||||
|
|
||||||
HttpResponse::Ok().body(format!(
|
HttpResponse::Ok().body(format!("Num of requests: {}", req.state().counter.get()))
|
||||||
"Num of requests: {}",
|
|
||||||
req.state().counter.get()
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -2,14 +2,10 @@
|
|||||||
name = "static_index"
|
name = "static_index"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Jose Marinez <digeratus@gmail.com>"]
|
authors = ["Jose Marinez <digeratus@gmail.com>"]
|
||||||
|
workspace = "../"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
env_logger = "0.5"
|
env_logger = "0.5"
|
||||||
actix = "^0.5.5"
|
actix = "0.5"
|
||||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
actix-web = "0.6"
|
||||||
|
|
||||||
[workspace]
|
|
||||||
members = [
|
|
||||||
"./"
|
|
||||||
]
|
|
||||||
|
@ -2,7 +2,7 @@ extern crate actix;
|
|||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
use actix_web::{fs, App, server, middleware};
|
use actix_web::{fs, middleware, server, App};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
@ -11,22 +11,18 @@ fn main() {
|
|||||||
|
|
||||||
let sys = actix::System::new("static_index");
|
let sys = actix::System::new("static_index");
|
||||||
|
|
||||||
|
server::new(|| {
|
||||||
server::new(
|
App::new()
|
||||||
|| App::new()
|
|
||||||
// enable logger
|
// enable logger
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.handler(
|
.handler(
|
||||||
"/",
|
"/",
|
||||||
fs::StaticFiles::new("./static/").index_file("index.html")
|
fs::StaticFiles::new("./static/").index_file("index.html")
|
||||||
)
|
)
|
||||||
)
|
}).bind("127.0.0.1:8080")
|
||||||
|
.expect("Can not start server on given IP/Port")
|
||||||
.bind("127.0.0.1:8080").expect( "Can not start server on given IP/Port" )
|
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
println!("Started http server: 127.0.0.1:8080");
|
println!("Started http server: 127.0.0.1:8080");
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,9 @@ extern crate tera;
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use actix_web::{error, http, middleware, server, App, Error, HttpResponse, Query, State};
|
use actix_web::{
|
||||||
|
error, http, middleware, server, App, Error, HttpResponse, Query, State,
|
||||||
|
};
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
template: tera::Tera, // <- store tera template in application state
|
template: tera::Tera, // <- store tera template in application state
|
||||||
|
@ -27,9 +27,7 @@ fn main() {
|
|||||||
builder
|
builder
|
||||||
.set_private_key_file("key.pem", SslFiletype::PEM)
|
.set_private_key_file("key.pem", SslFiletype::PEM)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
builder
|
builder.set_certificate_chain_file("cert.pem").unwrap();
|
||||||
.set_certificate_chain_file("cert.pem")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
server::new(|| {
|
server::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -7,11 +7,9 @@ extern crate futures;
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use actix_web::{http::{header, Method},
|
use actix_web::{
|
||||||
middleware,
|
http::{header, Method}, middleware, middleware::cors::Cors, server, App,
|
||||||
middleware::cors::Cors,
|
};
|
||||||
server,
|
|
||||||
App};
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
mod user;
|
mod user;
|
||||||
|
@ -83,9 +83,7 @@ impl Actor for WsChatSession {
|
|||||||
|
|
||||||
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
|
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
|
||||||
// notify chat server
|
// notify chat server
|
||||||
ctx.state()
|
ctx.state().addr.do_send(server::Disconnect { id: self.id });
|
||||||
.addr
|
|
||||||
.do_send(server::Disconnect { id: self.id });
|
|
||||||
Running::Stop
|
Running::Stop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,10 +114,7 @@ impl Handler<Connect> for ChatServer {
|
|||||||
self.sessions.insert(id, msg.addr);
|
self.sessions.insert(id, msg.addr);
|
||||||
|
|
||||||
// auto join session to Main room
|
// auto join session to Main room
|
||||||
self.rooms
|
self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id);
|
||||||
.get_mut(&"Main".to_owned())
|
|
||||||
.unwrap()
|
|
||||||
.insert(id);
|
|
||||||
|
|
||||||
// send id back
|
// send id back
|
||||||
id
|
id
|
||||||
|
@ -121,8 +121,7 @@ impl Handler<ClientCommand> for ChatClient {
|
|||||||
}
|
}
|
||||||
"/join" => {
|
"/join" => {
|
||||||
if v.len() == 2 {
|
if v.len() == 2 {
|
||||||
self.framed
|
self.framed.write(codec::ChatRequest::Join(v[1].to_owned()));
|
||||||
.write(codec::ChatRequest::Join(v[1].to_owned()));
|
|
||||||
} else {
|
} else {
|
||||||
println!("!!! room name is required");
|
println!("!!! room name is required");
|
||||||
}
|
}
|
||||||
@ -130,8 +129,7 @@ impl Handler<ClientCommand> for ChatClient {
|
|||||||
_ => println!("!!! unknown command"),
|
_ => println!("!!! unknown command"),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.framed
|
self.framed.write(codec::ChatRequest::Message(m.to_owned()));
|
||||||
.write(codec::ChatRequest::Message(m.to_owned()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ impl Encoder for ChatCodec {
|
|||||||
let msg_ref: &[u8] = msg.as_ref();
|
let msg_ref: &[u8] = msg.as_ref();
|
||||||
|
|
||||||
dst.reserve(msg_ref.len() + 2);
|
dst.reserve(msg_ref.len() + 2);
|
||||||
dst.put_u16::<BigEndian>(msg_ref.len() as u16);
|
dst.put_u16_be(msg_ref.len() as u16);
|
||||||
dst.put(msg_ref);
|
dst.put(msg_ref);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -114,7 +114,7 @@ impl Encoder for ClientChatCodec {
|
|||||||
let msg_ref: &[u8] = msg.as_ref();
|
let msg_ref: &[u8] = msg.as_ref();
|
||||||
|
|
||||||
dst.reserve(msg_ref.len() + 2);
|
dst.reserve(msg_ref.len() + 2);
|
||||||
dst.put_u16::<BigEndian>(msg_ref.len() as u16);
|
dst.put_u16_be(msg_ref.len() as u16);
|
||||||
dst.put(msg_ref);
|
dst.put(msg_ref);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -19,7 +19,6 @@ use std::time::Instant;
|
|||||||
|
|
||||||
use actix::*;
|
use actix::*;
|
||||||
use actix_web::server::HttpServer;
|
use actix_web::server::HttpServer;
|
||||||
use actix_web::ws::WsWriter;
|
|
||||||
use actix_web::{fs, http, ws, App, Error, HttpRequest, HttpResponse};
|
use actix_web::{fs, http, ws, App, Error, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
mod codec;
|
mod codec;
|
||||||
@ -88,9 +87,7 @@ impl Actor for WsChatSession {
|
|||||||
|
|
||||||
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
|
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
|
||||||
// notify chat server
|
// notify chat server
|
||||||
ctx.state()
|
ctx.state().addr.do_send(server::Disconnect { id: self.id });
|
||||||
.addr
|
|
||||||
.do_send(server::Disconnect { id: self.id });
|
|
||||||
Running::Stop
|
Running::Stop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,7 @@ impl Handler<Connect> for ChatServer {
|
|||||||
self.sessions.insert(id, msg.addr);
|
self.sessions.insert(id, msg.addr);
|
||||||
|
|
||||||
// auto join session to Main room
|
// auto join session to Main room
|
||||||
self.rooms
|
self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id);
|
||||||
.get_mut(&"Main".to_owned())
|
|
||||||
.unwrap()
|
|
||||||
.insert(id);
|
|
||||||
|
|
||||||
// send id back
|
// send id back
|
||||||
id
|
id
|
||||||
|
@ -9,8 +9,9 @@ extern crate actix_web;
|
|||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use actix_web::ws::WsWriter;
|
use actix_web::{
|
||||||
use actix_web::{fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse};
|
fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
/// do websocket handshake and start `MyWebSocket` actor
|
/// do websocket handshake and start `MyWebSocket` actor
|
||||||
fn ws_index(r: HttpRequest) -> Result<HttpResponse, Error> {
|
fn ws_index(r: HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
|
Loading…
Reference in New Issue
Block a user