2018-09-18 11:19:45 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
use actix::fut;
|
|
|
|
use actix::prelude::*;
|
|
|
|
use actix_broker::BrokerIssue;
|
2019-07-11 15:02:25 +06:00
|
|
|
use actix_files::Files;
|
|
|
|
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
|
|
|
use actix_web_actors::ws;
|
2018-09-18 11:19:45 +01:00
|
|
|
|
|
|
|
mod server;
|
|
|
|
use server::*;
|
|
|
|
|
2019-07-11 15:02:25 +06:00
|
|
|
fn chat_route(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
|
|
|
ws::start(WsChatSession::default(), &req, stream)
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct WsChatSession {
|
|
|
|
id: usize,
|
|
|
|
room: String,
|
|
|
|
name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WsChatSession {
|
|
|
|
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.
|
2019-07-11 15:02:25 +06:00
|
|
|
self.issue_system_sync(leave_msg, ctx);
|
2018-09-18 11:19:45 +01:00
|
|
|
// 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::ok(())
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
|
|
|
.spawn(ctx);
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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::ok(())
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
|
|
|
.spawn(ctx);
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn send_msg(&self, msg: &str) {
|
|
|
|
let content = format!(
|
|
|
|
"{}: {}",
|
2019-09-05 00:04:57 +09:00
|
|
|
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
2018-09-18 11:19:45 +01:00
|
|
|
msg
|
|
|
|
);
|
|
|
|
let msg = SendMessage(self.room.clone(), self.id, content);
|
|
|
|
// issue_async comes from having the `BrokerIssue` trait in scope.
|
2019-07-11 15:02:25 +06:00
|
|
|
self.issue_system_async(msg);
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
info!(
|
|
|
|
"WsChatSession closed for {}({}) in room {}",
|
2019-09-05 00:04:57 +09:00
|
|
|
self.name.clone().unwrap_or_else(|| "anon".to_string()),
|
2018-09-18 11:19:45 +01:00
|
|
|
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<ws::Message, ws::ProtocolError> for WsChatSession {
|
|
|
|
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
|
|
|
debug!("WEBSOCKET MESSAGE: {:?}", msg);
|
|
|
|
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());
|
|
|
|
ctx.text(format!("name changed to: {}", name));
|
|
|
|
} else {
|
|
|
|
ctx.text("!!! name is required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ctx.text(format!("!!! unknown command: {:?}", msg)),
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.send_msg(msg);
|
|
|
|
}
|
|
|
|
ws::Message::Close(_) => {
|
|
|
|
ctx.stop();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:02:25 +06:00
|
|
|
fn main() -> std::io::Result<()> {
|
2018-09-18 11:19:45 +01:00
|
|
|
let sys = actix::System::new("websocket-broker-example");
|
|
|
|
simple_logger::init_with_level(log::Level::Info).unwrap();
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2019-07-11 15:02:25 +06:00
|
|
|
.service(web::resource("/ws/").to(chat_route))
|
|
|
|
.service(Files::new("/", "./static/").index_file("index.html"))
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")
|
2018-09-18 11:19:45 +01:00
|
|
|
.unwrap()
|
|
|
|
.start();
|
|
|
|
|
|
|
|
info!("Started http server: 127.0.0.1:8080");
|
2019-07-11 15:02:25 +06:00
|
|
|
sys.run()
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|