Elegent shutdown the agent

This commit is contained in:
Jovi Hsu 2021-11-17 09:34:25 +08:00
parent 19dd409744
commit 4aa8c19adb
2 changed files with 10 additions and 2 deletions

View File

@ -65,7 +65,7 @@ impl Actor for Agent {
impl Handler<AgentMsg> for Agent { impl Handler<AgentMsg> for Agent {
type Result = (); type Result = ();
fn handle(&mut self, msg: AgentMsg, _ctx: &mut Context<Self>) -> Self::Result { fn handle(&mut self, msg: AgentMsg, ctx: &mut Context<Self>) -> Self::Result {
match msg { match msg {
AgentMsg::Ready(ws_addr) => { AgentMsg::Ready(ws_addr) => {
self.ws_addr = Some(ws_addr); self.ws_addr = Some(ws_addr);
@ -89,7 +89,10 @@ impl Handler<AgentMsg> for Agent {
.do_send(ws::WsMsg::SendToClient(data)); .do_send(ws::WsMsg::SendToClient(data));
} }
} }
_ => panic!("unexpected message"), AgentMsg::Shutdown => {
info!("Agent {} - Shutdown", self.server_info);
ctx.stop();
}
} }
} }
} }

View File

@ -1,5 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use actix::ActorContext;
use actix::{Actor, Addr, Message, StreamHandler}; use actix::{Actor, Addr, Message, StreamHandler};
use actix::{AsyncContext, Handler}; use actix::{AsyncContext, Handler};
use actix_session::Session; use actix_session::Session;
@ -53,6 +54,10 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
Ok(ws::Message::Binary(bin)) => { Ok(ws::Message::Binary(bin)) => {
self.agent.do_send(AgentMsg::SendToServer(bin)); self.agent.do_send(AgentMsg::SendToServer(bin));
} }
Ok(ws::Message::Close(_)) => {
self.agent.do_send(AgentMsg::Shutdown);
ctx.stop();
}
_ => (), _ => (),
} }
} }