2022-02-18 01:44:53 +00:00
|
|
|
use actix::{fut, prelude::*};
|
2020-04-09 01:54:28 +01:00
|
|
|
use actix_broker::BrokerIssue;
|
|
|
|
use actix_web_actors::ws;
|
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
use crate::{
|
|
|
|
message::{ChatMessage, JoinRoom, LeaveRoom, ListRooms, SendMessage},
|
|
|
|
server::WsChatServer,
|
|
|
|
};
|
2020-04-09 01:54:28 +01:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WsChatSession {
|
|
|
|
id: usize,
|
|
|
|
room: String,
|
|
|
|
name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WsChatSession {
|
|
|
|
pub fn join_room(&mut self, room_name: &str, ctx: &mut ws::WebsocketContext<Self>) {
|
|
|
|
let room_name = room_name.to_owned();
|
|
|
|
|
|
|
|
// First send a leave message for the current room
|
|
|
|
let leave_msg = LeaveRoom(self.room.clone(), self.id);
|
|
|
|
|
|
|
|
// issue_sync comes from having the `BrokerIssue` trait in scope.
|
|
|
|
self.issue_system_sync(leave_msg, ctx);
|
|
|
|
|
|
|
|
// Then send a join message for the new room
|
|
|
|
let join_msg = JoinRoom(
|
|
|
|
room_name.to_owned(),
|
|
|
|
self.name.clone(),
|
|
|
|
ctx.address().recipient(),
|
|
|
|
);
|
|
|
|
|
|
|
|
WsChatServer::from_registry()
|
|
|
|
.send(join_msg)
|
|
|
|
.into_actor(self)
|
|
|
|
.then(|id, act, _ctx| {
|
|
|
|
if let Ok(id) = id {
|
|
|
|
act.id = id;
|
|
|
|
act.room = room_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
fut::ready(())
|
|
|
|
})
|
|
|
|
.wait(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_rooms(&mut self, ctx: &mut ws::WebsocketContext<Self>) {
|
|
|
|
WsChatServer::from_registry()
|
|
|
|
.send(ListRooms)
|
|
|
|
.into_actor(self)
|
|
|
|
.then(|res, _, ctx| {
|
|
|
|
if let Ok(rooms) = res {
|
|
|
|
for room in rooms {
|
|
|
|
ctx.text(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fut::ready(())
|
|
|
|
})
|
|
|
|
.wait(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_msg(&self, msg: &str) {
|
|
|
|
let content = format!(
|
2022-06-07 22:53:38 -04:00
|
|
|
"{}: {msg}",
|
2020-04-09 01:54:28 +01:00
|
|
|
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
|
|
|
);
|
|
|
|
|
|
|
|
let msg = SendMessage(self.room.clone(), self.id, content);
|
|
|
|
|
|
|
|
// issue_async comes from having the `BrokerIssue` trait in scope.
|
|
|
|
self.issue_system_async(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Actor for WsChatSession {
|
|
|
|
type Context = ws::WebsocketContext<Self>;
|
|
|
|
|
|
|
|
fn started(&mut self, ctx: &mut Self::Context) {
|
|
|
|
self.join_room("Main", ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stopped(&mut self, _ctx: &mut Self::Context) {
|
2022-02-18 01:44:53 +00:00
|
|
|
log::info!(
|
2020-04-09 01:54:28 +01:00
|
|
|
"WsChatSession closed for {}({}) in room {}",
|
|
|
|
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
|
|
|
self.id,
|
|
|
|
self.room
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<ChatMessage> for WsChatSession {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: ChatMessage, ctx: &mut Self::Context) {
|
|
|
|
ctx.text(msg.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsChatSession {
|
2022-02-18 02:44:02 +00:00
|
|
|
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
2020-04-09 01:54:28 +01:00
|
|
|
let msg = match msg {
|
|
|
|
Err(_) => {
|
|
|
|
ctx.stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok(msg) => msg,
|
|
|
|
};
|
|
|
|
|
2022-06-07 22:53:38 -04:00
|
|
|
log::debug!("WEBSOCKET MESSAGE: {msg:?}");
|
2020-04-09 01:54:28 +01:00
|
|
|
|
|
|
|
match msg {
|
|
|
|
ws::Message::Text(text) => {
|
|
|
|
let msg = text.trim();
|
|
|
|
|
|
|
|
if msg.starts_with('/') {
|
|
|
|
let mut command = msg.splitn(2, ' ');
|
|
|
|
|
|
|
|
match command.next() {
|
|
|
|
Some("/list") => self.list_rooms(ctx),
|
|
|
|
|
|
|
|
Some("/join") => {
|
|
|
|
if let Some(room_name) = command.next() {
|
|
|
|
self.join_room(room_name, ctx);
|
|
|
|
} else {
|
|
|
|
ctx.text("!!! room name is required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some("/name") => {
|
|
|
|
if let Some(name) = command.next() {
|
|
|
|
self.name = Some(name.to_owned());
|
2022-06-07 22:53:38 -04:00
|
|
|
ctx.text(format!("name changed to: {name}"));
|
2020-04-09 01:54:28 +01:00
|
|
|
} else {
|
|
|
|
ctx.text("!!! name is required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 22:53:38 -04:00
|
|
|
_ => ctx.text(format!("!!! unknown command: {msg:?}")),
|
2020-04-09 01:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.send_msg(msg);
|
|
|
|
}
|
2020-06-25 06:20:27 +02:00
|
|
|
ws::Message::Close(reason) => {
|
|
|
|
ctx.close(reason);
|
2020-04-09 01:54:28 +01:00
|
|
|
ctx.stop();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|