#![allow(unused)]
fn main() {
use actix::prelude::*;
use std::time::Duration;
#[derive(Message)]
#[rtype(result = "()")]
struct Ping {
pub id: usize,
}
struct Game {
counter: usize,
name: String,
// 给其他actor发送
recipient: Recipient<Ping>,
}
impl Actor for Game {
type Context = Context<Game>;
}
impl Handler<Ping> for Game {
type Result = ();
fn handle(&mut self, msg: Ping, ctx: &mut Context<Self>) {
self.counter += 1;
if self.counter > 10 {
System::current().stop();
} else {
println!("[{0}] Ping received {1}", self.name, msg.id);
ctx.run_later(Duration::new(0, 100), move |act, _| {
// 给recipient发 在这个例子里就是 另一个Game Actor
act.recipient.do_send(Ping { id: msg.id + 1 });
});
}
}
}
}
///
/// game 互啄
fn main() {
let mut system = System::new();
let addr = system.block_on(async {
Game::create(|ctx| {
// game1 的 addr
let addr = ctx.address();
// game2
let addr2 = Game {
counter: 0,
name: String::from("Game 2"),
// game1 的 recipient
recipient: addr.recipient(),
}
.start();
// game2 先发送
addr2.do_send(Ping { id: 10 });
Game {
counter: 0,
name: String::from("Game 1"),
recipient: addr2.recipient(),
}
});
});
system.run();
}