mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 09:42:40 +01:00
update examples
This commit is contained in:
parent
b1eec3131f
commit
96b87761d1
@ -38,7 +38,7 @@ struct State {
|
|||||||
fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
let name = &req.match_info()["name"];
|
let name = &req.match_info()["name"];
|
||||||
|
|
||||||
req.state().db.call(CreateUser{name: name.to_owned()})
|
req.state().db.send(CreateUser{name: name.to_owned()})
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(|res| {
|
.and_then(|res| {
|
||||||
match res {
|
match res {
|
||||||
|
@ -45,7 +45,7 @@ fn main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
addr.send(ClientCommand(cmd));
|
addr.do_send(ClientCommand(cmd));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ impl Actor for ChatClient {
|
|||||||
println!("Disconnected");
|
println!("Disconnected");
|
||||||
|
|
||||||
// Stop application on disconnect
|
// Stop application on disconnect
|
||||||
Arbiter::system().send(actix::msgs::SystemExit(0));
|
Arbiter::system().do_send(actix::msgs::SystemExit(0));
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ impl Actor for WsChatSession {
|
|||||||
// HttpContext::state() is instance of WsChatSessionState, state is shared across all
|
// HttpContext::state() is instance of WsChatSessionState, state is shared across all
|
||||||
// routes within application
|
// routes within application
|
||||||
let addr: Addr<Syn, _> = ctx.address();
|
let addr: Addr<Syn, _> = ctx.address();
|
||||||
ctx.state().addr.call(server::Connect{addr: addr.subscriber()})
|
ctx.state().addr.send(server::Connect{addr: addr.recipient()})
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.then(|res, act, ctx| {
|
.then(|res, act, ctx| {
|
||||||
match res {
|
match res {
|
||||||
@ -77,7 +77,7 @@ impl Actor for WsChatSession {
|
|||||||
|
|
||||||
fn stopping(&mut self, ctx: &mut Self::Context) -> bool {
|
fn stopping(&mut self, ctx: &mut Self::Context) -> bool {
|
||||||
// notify chat server
|
// notify chat server
|
||||||
ctx.state().addr.send(server::Disconnect{id: self.id});
|
ctx.state().addr.do_send(server::Disconnect{id: self.id});
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ impl Handler<ws::Message> for WsChatSession {
|
|||||||
"/list" => {
|
"/list" => {
|
||||||
// Send ListRooms message to chat server and wait for response
|
// Send ListRooms message to chat server and wait for response
|
||||||
println!("List rooms");
|
println!("List rooms");
|
||||||
ctx.state().addr.call(server::ListRooms)
|
ctx.state().addr.send(server::ListRooms)
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.then(|res, _, ctx| {
|
.then(|res, _, ctx| {
|
||||||
match res {
|
match res {
|
||||||
@ -129,7 +129,7 @@ impl Handler<ws::Message> for WsChatSession {
|
|||||||
"/join" => {
|
"/join" => {
|
||||||
if v.len() == 2 {
|
if v.len() == 2 {
|
||||||
self.room = v[1].to_owned();
|
self.room = v[1].to_owned();
|
||||||
ctx.state().addr.send(
|
ctx.state().addr.do_send(
|
||||||
server::Join{id: self.id, name: self.room.clone()});
|
server::Join{id: self.id, name: self.room.clone()});
|
||||||
|
|
||||||
ctx.text("joined");
|
ctx.text("joined");
|
||||||
@ -153,7 +153,7 @@ impl Handler<ws::Message> for WsChatSession {
|
|||||||
m.to_owned()
|
m.to_owned()
|
||||||
};
|
};
|
||||||
// send message to chat server
|
// send message to chat server
|
||||||
ctx.state().addr.send(
|
ctx.state().addr.do_send(
|
||||||
server::Message{id: self.id,
|
server::Message{id: self.id,
|
||||||
msg: msg,
|
msg: msg,
|
||||||
room: self.room.clone()})
|
room: self.room.clone()})
|
||||||
@ -178,7 +178,7 @@ fn main() {
|
|||||||
|
|
||||||
// Start tcp server in separate thread
|
// Start tcp server in separate thread
|
||||||
let srv = server.clone();
|
let srv = server.clone();
|
||||||
Arbiter::new("tcp-server").send::<msgs::Execute>(
|
Arbiter::new("tcp-server").do_send::<msgs::Execute>(
|
||||||
msgs::Execute::new(move || {
|
msgs::Execute::new(move || {
|
||||||
session::TcpServer::new("127.0.0.1:12345", srv);
|
session::TcpServer::new("127.0.0.1:12345", srv);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -15,7 +15,7 @@ use session;
|
|||||||
#[derive(Message)]
|
#[derive(Message)]
|
||||||
#[rtype(usize)]
|
#[rtype(usize)]
|
||||||
pub struct Connect {
|
pub struct Connect {
|
||||||
pub addr: Subscriber<Syn, session::Message>,
|
pub addr: Recipient<Syn, session::Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Session is disconnected
|
/// Session is disconnected
|
||||||
@ -54,7 +54,7 @@ pub struct Join {
|
|||||||
/// `ChatServer` manages chat rooms and responsible for coordinating chat session.
|
/// `ChatServer` manages chat rooms and responsible for coordinating chat session.
|
||||||
/// implementation is super primitive
|
/// implementation is super primitive
|
||||||
pub struct ChatServer {
|
pub struct ChatServer {
|
||||||
sessions: HashMap<usize, Subscriber<Syn, session::Message>>,
|
sessions: HashMap<usize, Recipient<Syn, session::Message>>,
|
||||||
rooms: HashMap<String, HashSet<usize>>,
|
rooms: HashMap<String, HashSet<usize>>,
|
||||||
rng: RefCell<ThreadRng>,
|
rng: RefCell<ThreadRng>,
|
||||||
}
|
}
|
||||||
@ -80,7 +80,7 @@ impl ChatServer {
|
|||||||
for id in sessions {
|
for id in sessions {
|
||||||
if *id != skip_id {
|
if *id != skip_id {
|
||||||
if let Some(addr) = self.sessions.get(id) {
|
if let Some(addr) = self.sessions.get(id) {
|
||||||
let _ = addr.send(session::Message(message.to_owned()));
|
let _ = addr.do_send(session::Message(message.to_owned()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ impl Actor for ChatSession {
|
|||||||
// future within context, but context waits until this future resolves
|
// future within context, but context waits until this future resolves
|
||||||
// before processing any other events.
|
// before processing any other events.
|
||||||
let addr: Addr<Syn, _> = ctx.address();
|
let addr: Addr<Syn, _> = ctx.address();
|
||||||
self.addr.call(server::Connect{addr: addr.subscriber()})
|
self.addr.send(server::Connect{addr: addr.recipient()})
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.then(|res, act, ctx| {
|
.then(|res, act, ctx| {
|
||||||
match res {
|
match res {
|
||||||
@ -60,7 +60,7 @@ impl Actor for ChatSession {
|
|||||||
|
|
||||||
fn stopping(&mut self, ctx: &mut Self::Context) -> bool {
|
fn stopping(&mut self, ctx: &mut Self::Context) -> bool {
|
||||||
// notify chat server
|
// notify chat server
|
||||||
self.addr.send(server::Disconnect{id: self.id});
|
self.addr.do_send(server::Disconnect{id: self.id});
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ impl StreamHandler<ChatRequest, io::Error> for ChatSession {
|
|||||||
ChatRequest::List => {
|
ChatRequest::List => {
|
||||||
// Send ListRooms message to chat server and wait for response
|
// Send ListRooms message to chat server and wait for response
|
||||||
println!("List rooms");
|
println!("List rooms");
|
||||||
self.addr.call(server::ListRooms)
|
self.addr.send(server::ListRooms)
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.then(|res, act, ctx| {
|
.then(|res, act, ctx| {
|
||||||
match res {
|
match res {
|
||||||
@ -93,13 +93,13 @@ impl StreamHandler<ChatRequest, io::Error> for ChatSession {
|
|||||||
ChatRequest::Join(name) => {
|
ChatRequest::Join(name) => {
|
||||||
println!("Join to room: {}", name);
|
println!("Join to room: {}", name);
|
||||||
self.room = name.clone();
|
self.room = name.clone();
|
||||||
self.addr.send(server::Join{id: self.id, name: name.clone()});
|
self.addr.do_send(server::Join{id: self.id, name: name.clone()});
|
||||||
self.framed.write(ChatResponse::Joined(name));
|
self.framed.write(ChatResponse::Joined(name));
|
||||||
},
|
},
|
||||||
ChatRequest::Message(message) => {
|
ChatRequest::Message(message) => {
|
||||||
// send message to chat server
|
// send message to chat server
|
||||||
println!("Peer message: {}", message);
|
println!("Peer message: {}", message);
|
||||||
self.addr.send(
|
self.addr.do_send(
|
||||||
server::Message{id: self.id,
|
server::Message{id: self.id,
|
||||||
msg: message, room:
|
msg: message, room:
|
||||||
self.room.clone()})
|
self.room.clone()})
|
||||||
@ -141,7 +141,7 @@ impl ChatSession {
|
|||||||
println!("Client heartbeat failed, disconnecting!");
|
println!("Client heartbeat failed, disconnecting!");
|
||||||
|
|
||||||
// notify chat server
|
// notify chat server
|
||||||
act.addr.send(server::Disconnect{id: act.id});
|
act.addr.do_send(server::Disconnect{id: act.id});
|
||||||
|
|
||||||
// stop actor
|
// stop actor
|
||||||
ctx.stop();
|
ctx.stop();
|
||||||
|
@ -41,7 +41,7 @@ fn main() {
|
|||||||
println!("error");
|
println!("error");
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
addr.send(ClientCommand(cmd));
|
addr.do_send(ClientCommand(cmd));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ impl Actor for ChatClient {
|
|||||||
println!("Stopping");
|
println!("Stopping");
|
||||||
|
|
||||||
// Stop application on disconnect
|
// Stop application on disconnect
|
||||||
Arbiter::system().send(actix::msgs::SystemExit(0));
|
Arbiter::system().do_send(actix::msgs::SystemExit(0));
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user