error catching for sockets
This commit is contained in:
parent
686d7b8237
commit
6a1f512db4
1 changed files with 20 additions and 7 deletions
|
@ -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 => {
|
||||||
|
removes.push(i);
|
||||||
|
},
|
||||||
|
Some(strm) => {
|
||||||
|
let mut tx = strm.as_ref();
|
||||||
match tx.write(message.clone().as_bytes()) {
|
match tx.write(message.clone().as_bytes()) {
|
||||||
Err(_) => {streams.remove(i); println!("removed a socket"); continue;},
|
Err(_) => {
|
||||||
Ok(_) => {},
|
removes.push(i)
|
||||||
}
|
},
|
||||||
|
Ok(_) => {
|
||||||
tx.write(b"\n").unwrap();
|
tx.write(b"\n").unwrap();
|
||||||
tx.flush().unwrap();
|
tx.flush().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i in removes.iter() {
|
||||||
|
streams.remove(*i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue