make net ws more robust to test websocket server connection failure

This commit is contained in:
nworbnhoj 2023-12-12 16:12:22 +11:00 committed by GitHub
parent fe2eb732a0
commit 7a75bf73e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -415,9 +415,9 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
#[cfg(feature = "websocket")] #[cfg(feature = "websocket")]
"ws" => { "ws" => {
if self.ws.is_none() { if self.ws.is_none() {
let (socket, response) = let url = url::Url::parse("wss://awake.noskills.club/ws").expect("Can't parse");
tungstenite::connect(url::Url::parse("wss://awake.noskills.club/ws").unwrap()).expect("Can't connect"); match tungstenite::connect(url) {
Ok((socket, response)) => {
log::info!("Connected to the server"); log::info!("Connected to the server");
log::info!("Response HTTP code: {}", response.status()); log::info!("Response HTTP code: {}", response.status());
log::info!("Response contains the following headers:"); log::info!("Response contains the following headers:");
@ -426,6 +426,12 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
} }
self.ws = Some(socket); self.ws = Some(socket);
} }
Err(e) => {
log::warn!("failed to connect to ws server: {e}");
write!(ret, "failed to connect to ws server: {e}").ok();
}
}
}
let mut err = false; let mut err = false;
if let Some(socket) = &mut self.ws { if let Some(socket) = &mut self.ws {
let mut val = String::<1024>::new(); let mut val = String::<1024>::new();