This commit is contained in:
Piyush मिश्रः 2021-05-14 22:46:51 +05:30
parent e49d0df690
commit 04e81e3ec0
4 changed files with 47 additions and 13 deletions

View File

@ -66,7 +66,7 @@ async fn main() -> std::io::Result<()> {
let mut redirect = None; let mut redirect = None;
let port_x = config.port_x.clone(); let port_x = config.port_x.clone();
let port = config.port.clone(); let port = config.port.clone();
if ssl_builder.is_some() { if ssl_builder.is_some() && config.port_x != "" {
redirect = Some(HttpServer::new(move || { redirect = Some(HttpServer::new(move || {
App::new() App::new()
.wrap( .wrap(
@ -99,16 +99,13 @@ async fn main() -> std::io::Result<()> {
.service(fs::Files::new("/", &static_path).index_file("index.html")) .service(fs::Files::new("/", &static_path).index_file("index.html"))
}); });
match ssl_builder { if ssl_builder.is_some() && config.port_x != "" {
Some(b) => { let srv = server.bind_openssl(format!("{}:{}", config.bind_address, config.port), ssl_builder.unwrap())?.run();
let srv = server.bind_openssl(format!("{}:{}", config.bind_address, config.port), b)?.run(); tokio::try_join!(redirect.unwrap(), srv)?;
tokio::try_join!(redirect.unwrap(), srv)?; } else {
}, server.bind(format!("{}:{}", config.bind_address, config.port))?.run().await?;
None => {
server.bind(format!("{}:{}", config.bind_address, config.port))?.run().await?;
}
} }
Ok(()) Ok(())
} }

View File

@ -34,11 +34,17 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
/// How long before lack of client response causes a timeout /// How long before lack of client response causes a timeout
const CLIENT_TIMEOUT: Duration = Duration::from_secs(15); const CLIENT_TIMEOUT: Duration = Duration::from_secs(15);
/// How often heartbeat pings are sent
const SPECIAL_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5 * 60);
/// How long before lack of client response causes a timeout
const SPECIAL_CLIENT_TIMEOUT: Duration = Duration::from_secs(15 * 60);
pub struct WsSansad { pub struct WsSansad {
kunjika: String, kunjika: String,
isthiti: Isthiti, isthiti: Isthiti,
addr: Option<Addr<Self>>, addr: Option<Addr<Self>>,
hb: Instant hb: Instant,
special_hb: Instant,
} }
#[derive(Debug)] #[derive(Debug)]
@ -55,6 +61,7 @@ impl Actor for WsSansad {
fn started(&mut self, ctx: &mut Self::Context) { fn started(&mut self, ctx: &mut Self::Context) {
self.addr = Some(ctx.address().clone()); // own addr self.addr = Some(ctx.address().clone()); // own addr
self.hb(ctx); self.hb(ctx);
self.special_hb(ctx);
} }
fn stopping(&mut self, _: &mut Self::Context) -> Running { fn stopping(&mut self, _: &mut Self::Context) -> Running {
@ -75,6 +82,7 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSansad {
}, Ok(ws::Message::Pong(_)) => { }, Ok(ws::Message::Pong(_)) => {
self.hb = Instant::now(); self.hb = Instant::now();
}, Ok(ws::Message::Text(msg)) => { }, Ok(ws::Message::Text(msg)) => {
self.special_hb = Instant::now();
tokio::runtime::Runtime::new().unwrap() tokio::runtime::Runtime::new().unwrap()
.block_on(self.parse_text_handle(msg)); .block_on(self.parse_text_handle(msg));
}, Ok(ws::Message::Close(msg)) => { }, Ok(ws::Message::Close(msg)) => {
@ -93,7 +101,8 @@ impl WsSansad {
kunjika: String::new(), kunjika: String::new(),
isthiti: Isthiti::None, isthiti: Isthiti::None,
addr: None, addr: None,
hb: Instant::now() hb: Instant::now(),
special_hb: Instant::now()
} }
} }
@ -118,6 +127,25 @@ impl WsSansad {
}); });
} }
/// helper method that sends ping to client every second.
///
/// also this method checks heartbeats from client
fn special_hb(&self, ctx: &mut <Self as Actor>::Context) {
ctx.run_interval(SPECIAL_HEARTBEAT_INTERVAL, |act, ctx| {
// check client heartbeats
if Instant::now().duration_since(act.special_hb) > SPECIAL_CLIENT_TIMEOUT {
// heartbeat timed out
// stop actor
tokio::runtime::Runtime::new().unwrap()
.block_on(act.leave_kaksh());
ctx.stop();
// don't try to send a ping
return;
}
});
}
/// parse the request text from client /// parse the request text from client
async fn parse_text_handle(&mut self, msg: String) { async fn parse_text_handle(&mut self, msg: String) {
if let Ok(val) = serde_json::from_str::<Value>(&msg) { if let Ok(val) = serde_json::from_str::<Value>(&msg) {

View File

@ -44,6 +44,15 @@ socket.onerror = function(event) {
'page and if still don\'t work upgrade Web Browser'); 'page and if still don\'t work upgrade Web Browser');
} }
socket.onclose = function (e) {
if(actions.has_key('leave')) return;
actions.clear();
myinfo.kunjika = '';
myinfo.name = '';
State.login();
State.hideProgress();
}
// Listen for messages // Listen for messages
socket.onmessage = function(event) { socket.onmessage = function(event) {
var j = JSON.parse(event.data); var j = JSON.parse(event.data);

View File

@ -1,4 +1,4 @@
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches || getCookie('theme') == 'dark') { if (getCookie('theme') == 'dark' || window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
$('body').toggleClass('dark'); $('body').toggleClass('dark');
} }