error catching for sockets

This commit is contained in:
Nickiel12 2022-01-02 19:02:26 -08:00
parent 686d7b8237
commit 6a1f512db4

View file

@ -82,16 +82,29 @@ impl Socket {
pub fn send(&self, message: String) { pub fn send(&self, message: String) {
let mut streams = self.socket_txs.lock().unwrap(); let mut streams = self.socket_txs.lock().unwrap();
let mut removes = Vec::<usize>::new();
if streams.len() == 0 {return} if streams.len() == 0 {return}
for i in 0..streams.len(){ for i in 0..streams.len(){
let mut tx = streams.get(i).unwrap().as_ref(); match streams.get(i) {
None => {
match tx.write(message.clone().as_bytes()) { removes.push(i);
Err(_) => {streams.remove(i); println!("removed a socket"); continue;}, },
Ok(_) => {}, Some(strm) => {
let mut tx = strm.as_ref();
match tx.write(message.clone().as_bytes()) {
Err(_) => {
removes.push(i)
},
Ok(_) => {
tx.write(b"\n").unwrap();
tx.flush().unwrap();
}
}
}
} }
tx.write(b"\n").unwrap(); }
tx.flush().unwrap(); for i in removes.iter() {
streams.remove(*i);
} }
} }
} }