From b5c0bd67406a2d2f367282a741ba42e337787130 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Sun, 21 Feb 2021 20:58:37 +0530 Subject: [PATCH] almost finalised just image left --- src/chat_pinnd.rs | 88 ++++++++++++++++++++-------------------- src/messages.rs | 1 + src/ws_sansad.rs | 32 +++++++++++++-- static/css/style.css | 1 + static/img/back.svg | 95 ++++++++++++++++++++++++++++++++++++++++++++ static/img/next.svg | 95 ++++++++++++++++++++++++++++++++++++++++++++ static/index.html | 13 +++--- static/js/app.js | 25 +++++++++--- static/policy.html | 61 ++++++++++++++++++++++++++++ static/terms.html | 53 ++++++++++++++++++++++++ static/test.html | 19 --------- 11 files changed, 406 insertions(+), 77 deletions(-) create mode 100644 static/img/back.svg create mode 100644 static/img/next.svg create mode 100644 static/policy.html create mode 100644 static/terms.html delete mode 100644 static/test.html diff --git a/src/chat_pinnd.rs b/src/chat_pinnd.rs index ad4e521..1a90e0c 100644 --- a/src/chat_pinnd.rs +++ b/src/chat_pinnd.rs @@ -4,7 +4,6 @@ use std::{collections::HashMap, vec}; use actix::prelude::*; use actix_broker::BrokerSubscribe; -use ms::Resp; use crate::{ws_sansad, messages as ms}; @@ -51,12 +50,12 @@ impl Actor for ChatPinnd { /// Join grih impl Handler for ChatPinnd { - type Result = Resp; + type Result = ms::Resp; fn handle(&mut self, msg: ms::JoinGrih, _: &mut Self::Context) -> Self::Result { // check if user exist if let Some(_) = self.vyaktigat_waitlist.iter().position(|vk| vk.kunjika == msg.kunjika) { - return Resp::Err("Kunjika already exist".to_owned()); + return ms::Resp::Err("Kunjika already exist".to_owned()); } if let Some(_) = self.grih.iter().position(|(_,g)| { @@ -65,7 +64,7 @@ impl Handler for ChatPinnd { None => false } }) { - return Resp::Err("Kunjika already exist".to_owned()); + return ms::Resp::Err("Kunjika already exist".to_owned()); } // check if grih exist and add user @@ -74,7 +73,7 @@ impl Handler for ChatPinnd { // check if grih have no space left if let Some(n) = grih.length { if grih.loog.len() >= n { - return Resp::Err("Grih have no space".to_owned()); + return ms::Resp::Err("Grih have no space".to_owned()); } } @@ -101,7 +100,7 @@ impl Handler for ChatPinnd { } } - Resp::Ok + ms::Resp::Ok } } @@ -110,11 +109,11 @@ impl Handler for ChatPinnd { /// Check if watchlist is empty, if yes add the kunjika andaddr to watchlist /// if watchlist have people get 0th person an connect it impl Handler for ChatPinnd { - type Result = Resp; + type Result = ms::Resp; fn handle(&mut self, msg: ms::JoinRandom, _: &mut Self::Context) -> Self::Result { // check if user exist if let Some(_) = self.vyaktigat_waitlist.iter().position(|vk| vk.kunjika == msg.kunjika) { - return Resp::Err("Kunjika already exist".to_owned()); + return ms::Resp::Err("Kunjika already exist".to_owned()); } if let Some(_) = self.grih.iter().position(|(_,g)| { @@ -123,7 +122,7 @@ impl Handler for ChatPinnd { None => false } }) { - return Resp::Err("Kunjika already exist".to_owned()); + return ms::Resp::Err("Kunjika already exist".to_owned()); } // Check if watch list is empty @@ -134,7 +133,7 @@ impl Handler for ChatPinnd { name: msg.name, tags: msg.tags }); - return Resp::None; + return ms::Resp::None; } // connect person with tag @@ -153,7 +152,7 @@ impl Handler for ChatPinnd { name: msg.name, tags: msg.tags }); - return Resp::None; + return ms::Resp::None; } } } else { 0 }; @@ -178,13 +177,13 @@ impl Handler for ChatPinnd { grih_kunjika: group_kunjika }); - Resp::Ok + ms::Resp::Ok } } /// Next Random user impl Handler for ChatPinnd { - type Result = Resp; + type Result = ms::Resp; fn handle(&mut self, msg: ms::JoinRandomNext, _: &mut Self::Context) -> Self::Result { let grih = self.grih.get_mut(&msg.grih_kunjika).unwrap(); @@ -195,16 +194,25 @@ impl Handler for ChatPinnd { let tags; { - let loog = grih.loog.get(0).unwrap(); + let loog = grih.loog.get(loog_i).unwrap(); if let None = loog.tags { - return Resp::Err("You are not a randome vyakti!".to_owned()); + return ms::Resp::Err("You are not a randome vyakti!".to_owned()); } addr = loog.addr.clone(); name = loog.name.to_owned(); tags = loog.tags.clone().unwrap(); } + + // remove from old grih + grih.loog.remove(loog_i); + grih.loog.iter().for_each(|a| { + a.addr.do_send(ms::WsDisconnected { + kunjika: msg.kunjika.to_owned(), + name: name.to_owned() + }) + }); // Check if watch list is empty if self.vyaktigat_waitlist.len() == 0 { @@ -214,38 +222,31 @@ impl Handler for ChatPinnd { name, tags }); - return Resp::None; + return ms::Resp::None; } - // connect person with tag or to zero - let tags = tags.clone(); - let pos = match self.vyaktigat_waitlist.iter().position(|vk| { - match vk.tags.iter().position(|t| tags.contains(t)) { - Some(_) => true, - None => false + let pos = if tags.len() > 0 { + match self.vyaktigat_waitlist.iter().position(|vk| { + match vk.tags.iter().position(|t| tags.contains(t)) { + Some(_) => true, + None => false + } + }) { + Some(i) => i, + None => { + self.vyaktigat_waitlist.push(VyaktiWatchlist { + kunjika: msg.kunjika, + addr, + name, + tags + }); + return ms::Resp::None; + } } - }) { - Some(i) => i, - None => { - self.vyaktigat_waitlist.push(VyaktiWatchlist { - kunjika: msg.kunjika, - addr, - name, - tags - }); - return Resp::None; - } - }; - + } else { 0 }; let vayakti_watchlist = self.vyaktigat_waitlist.remove(pos); let group_kunjika = format!("gupt_{}>{}",msg.kunjika.to_owned(), vayakti_watchlist.kunjika); - grih.loog.remove(loog_i); - grih.loog.iter().for_each(|a| { - a.addr.do_send(ms::WsDisconnected { - kunjika: msg.kunjika.to_owned(), - name: name.to_owned() - }) - }); + let log_count = grih.loog.len(); drop(grih); if log_count == 0 { @@ -256,7 +257,6 @@ impl Handler for ChatPinnd { loog: vec![Loog::new(addr.clone(), msg.kunjika.to_owned(), name.to_owned(), Some(tags.clone())), Loog::new(vayakti_watchlist.addr.clone(), vayakti_watchlist.kunjika.to_owned(), vayakti_watchlist.name.to_owned(), Some(vayakti_watchlist.tags.clone()))] }); - // notify about connection addr.do_send(ms::WsConnectedRandom { name: vayakti_watchlist.name, @@ -270,7 +270,7 @@ impl Handler for ChatPinnd { grih_kunjika: group_kunjika }); - Resp::Ok + ms::Resp::Ok } } diff --git a/src/messages.rs b/src/messages.rs index 2b7bfbd..1ef25ed 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -128,6 +128,7 @@ pub struct WsConnectedRandom { pub grih_kunjika: String } //################################################## Helper ################################################## +#[derive(Debug)] pub enum Resp { Ok, Err(String), diff --git a/src/ws_sansad.rs b/src/ws_sansad.rs index 808030a..4365d03 100644 --- a/src/ws_sansad.rs +++ b/src/ws_sansad.rs @@ -13,11 +13,16 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); /// How long before lack of client response causes a timeout const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); +// for phones if browser kept websocket on +const SPECIAL_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5*60); +const SPECIAL_CLIENT_TIMEOUT: Duration = Duration::from_secs(6*60); + pub struct WsSansad { kunjika: String, isthiti: Isthiti, addr: Option>, - hb: Instant + hb: Instant, + special_hb: Instant } #[derive(Debug)] @@ -33,6 +38,7 @@ impl Actor for WsSansad { fn started(&mut self, ctx: &mut Self::Context) { self.addr = Some(ctx.address().clone()); // own addr self.hb(ctx); + self.special_hb(ctx); } fn stopping(&mut self, _: &mut Self::Context) -> Running { @@ -51,6 +57,7 @@ impl StreamHandler> for WsSansad { }, Ok(ws::Message::Pong(_)) => { self.hb = Instant::now(); }, Ok(ws::Message::Text(msg)) => { + self.special_hb = Instant::now(); futures::executor::block_on(self.parse_text_handle(msg)); }, Ok(ws::Message::Close(msg)) => { ctx.close(msg); @@ -160,7 +167,8 @@ impl WsSansad { kunjika: String::new(), isthiti: Isthiti::None, addr: None, - hb: Instant::now() + hb: Instant::now(), + special_hb: Instant::now() } } @@ -184,6 +192,24 @@ impl WsSansad { }); } + /// helper method that sends ping to client every second. + /// + /// also this method checks heartbeats from client + fn special_hb(&self, ctx: &mut ::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 + ctx.stop(); + + // don't try to send a ping + return; + } + }); + } + /// parse the request text from client async fn parse_text_handle(&mut self, msg: String) { if let Ok(val) = serde_json::from_str::(&msg) { @@ -300,7 +326,7 @@ impl WsSansad { }; // request - let result: Resp = ChatPinnd::from_registry().send(ms::JoinRandomNext{ + let result: Resp = ChatPinnd::from_registry().send(ms::JoinRandomNext { kunjika: self.kunjika.to_owned(), grih_kunjika: grih_kunjika.to_owned(), }).await.unwrap(); diff --git a/static/css/style.css b/static/css/style.css index fa3ba35..dce1afc 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -21,6 +21,7 @@ textarea { height: 10px !important; position: fixed; top: 0; left: 0; right: 0; + z-index: 99999; } .cover-screen { diff --git a/static/img/back.svg b/static/img/back.svg new file mode 100644 index 0000000..fa4c0d6 --- /dev/null +++ b/static/img/back.svg @@ -0,0 +1,95 @@ + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/static/img/next.svg b/static/img/next.svg new file mode 100644 index 0000000..ec7485e --- /dev/null +++ b/static/img/next.svg @@ -0,0 +1,95 @@ + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/static/index.html b/static/index.html index 34286a0..7e9e5a9 100644 --- a/static/index.html +++ b/static/index.html @@ -15,7 +15,7 @@
-
+
Lupt Chat
@@ -74,9 +74,12 @@
+
+
By tapping "Connect" button I agree with Terms of Service and Privacy Policy
+
@@ -85,11 +88,11 @@