export let socket;
export function wsConn(is_authenticated, user_id) {
if (is_authenticated !== 1) {
return;
}
socket = new WebSocket("ws://localhost:4000/ws")
socket.onopen = () => {
console.log("Successfully connected to websockets")
}
socket.onclose = event => {};
socket.onerror = error => {};
socket.onmessage = msg => {
let data = JSON.parse(msg.data);
switch (data.action) {
case "logout":
if (data.user_id === user_id) {
logout()
}
break;
default:
}
}
}
// let loginLink = document.getElementById("login-link");
// let vtLink = document.getElementById("vt-link");
//
// document.addEventListener("DOMContentLoaded", function () {
// if (localStorage.getItem("token") !== null) {
// loginLink.innerHTML = 'Logout';
// vtLink.classList.remove('d-none');
// } else {
// loginLink.innerHTML = 'Login';
// }
// loginLink.classList.remove('d-none')
// });
function logout() {
localStorage.removeItem("token");
localStorage.removeItem("token_expiry");
location.href = "/logout";
}