feat: use wss:// if https

This commit is contained in:
2023-06-13 21:31:06 +02:00
parent d90daa0423
commit 9a064a339e
3 changed files with 43 additions and 2 deletions

View File

@@ -15,8 +15,15 @@ pub struct WebsocketService {
impl WebsocketService {
pub fn connect() -> Self {
let hostname = web_sys::window().unwrap().location().hostname().unwrap();
let ws_addr = format!("ws://{hostname}:4000/ws");
let window = web_sys::window().expect("no global `window` exists");
let location = window.location();
let hostname = location.hostname().expect("should have a hostname");
let protocol = match location.protocol().expect("should have a protocol").as_str() {
"https:" => "wss",
_ => "ws",
};
let ws_addr = format!("{protocol}://{hostname}:4000/ws");
let ws = WebSocket::open(&ws_addr).unwrap();
log::info!("Opened websocket connection to {ws_addr}");