domus

One damus client to rule them all
git clone git://jb55.com/domus
Log | Files | Refs | README

mod.rs (1526B)


      1 use ewebsock::{WsMessage, WsReceiver, WsSender};
      2 
      3 use crate::{ClientMessage, Filter, Result};
      4 use std::fmt;
      5 use std::hash::{Hash, Hasher};
      6 
      7 pub mod message;
      8 pub mod pool;
      9 
     10 #[derive(Debug)]
     11 pub enum RelayStatus {
     12     Connected,
     13     Connecting,
     14     Disconnected,
     15 }
     16 
     17 pub struct Relay {
     18     pub url: String,
     19     pub status: RelayStatus,
     20     pub sender: WsSender,
     21     pub receiver: WsReceiver,
     22 }
     23 
     24 impl fmt::Debug for Relay {
     25     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     26         f.debug_struct("Relay")
     27             .field("url", &self.url)
     28             .field("status", &self.status)
     29             .finish()
     30     }
     31 }
     32 
     33 impl Hash for Relay {
     34     fn hash<H: Hasher>(&self, state: &mut H) {
     35         // Hashes the Relay by hashing the URL
     36         self.url.hash(state);
     37     }
     38 }
     39 
     40 impl PartialEq for Relay {
     41     fn eq(&self, other: &Self) -> bool {
     42         self.url == other.url
     43     }
     44 }
     45 
     46 impl Eq for Relay {}
     47 
     48 impl Relay {
     49     pub fn new(url: String, wakeup: impl Fn() + Send + Sync + 'static) -> Result<Self> {
     50         let status = RelayStatus::Connecting;
     51         let (sender, receiver) = ewebsock::connect_with_wakeup(&url, wakeup)?;
     52 
     53         Ok(Self {
     54             url,
     55             sender,
     56             receiver,
     57             status,
     58         })
     59     }
     60 
     61     pub fn send(&mut self, msg: &ClientMessage) {
     62         let txt = WsMessage::Text(msg.to_json());
     63         self.sender.send(txt);
     64     }
     65 
     66     pub fn subscribe(&mut self, subid: String, filters: Vec<Filter>) {
     67         self.send(&ClientMessage::req(subid, filters));
     68     }
     69 }