some changes

This commit is contained in:
Mekan1206
2026-08-15 17:51:20 +05:00
parent 8f3079801b
commit 6bcad79475
12 changed files with 933 additions and 3 deletions

80
src/utils/echo.js Normal file
View File

@@ -0,0 +1,80 @@
import Echo from "laravel-echo";
import Pusher from "pusher-js";
let echoClient = null;
let echoToken = null;
function getToken() {
const authTokenCookie = document.cookie
.split("; ")
.find((row) => row.startsWith("authToken="));
const guestTokenCookie = document.cookie
.split("; ")
.find((row) => row.startsWith("guestToken="));
if (authTokenCookie) return authTokenCookie.split("=")[1];
if (localStorage.getItem("authToken")) return localStorage.getItem("authToken");
if (guestTokenCookie) return guestTokenCookie.split("=")[1];
if (localStorage.getItem("guestToken")) return localStorage.getItem("guestToken");
return null;
}
function apiOrigin() {
const url = import.meta.env.VITE_API_URL || "https://mm.com.tm/api/v1/";
return url.replace(/\/?api\/v1\/?$/, "");
}
export function getChatEcho() {
const key = import.meta.env.VITE_REVERB_APP_KEY;
const token = getToken();
if (!key || !token) {
return null;
}
if (echoClient && echoToken !== token) {
echoClient.disconnect();
echoClient = null;
}
if (echoClient) {
return echoClient;
}
window.Pusher = Pusher;
const scheme = import.meta.env.VITE_REVERB_SCHEME || "http";
const port = Number(import.meta.env.VITE_REVERB_PORT || (scheme === "https" ? 443 : 8080));
const host = import.meta.env.VITE_REVERB_HOST || "127.0.0.1";
echoToken = token;
echoClient = new Echo({
broadcaster: "reverb",
key,
wsHost: host,
wsPort: port,
wssPort: port,
forceTLS: scheme === "https",
enabledTransports: ["ws", "wss"],
disableStats: true,
Pusher,
authEndpoint: `${apiOrigin()}/broadcasting/auth`,
auth: {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
},
});
return echoClient;
}
export function leaveChatChannel(conversationId) {
if (!echoClient || !conversationId) {
return;
}
echoClient.leave(`chat.${conversationId}`);
}