Fix WebSocket fallback URL and add debug logging
All checks were successful
Build Frontend / Build Frontend (push) Successful in 23s
All checks were successful
Build Frontend / Build Frontend (push) Successful in 23s
- Fixed fallback to use api.turbotrades.dev instead of window.location.host - Added console logging to debug WebSocket URL resolution - Simplified fallback logic - always use API URL domain - This fixes ws.turbotrades.dev connection errors
This commit is contained in:
@@ -31,35 +31,47 @@ export const useWebSocketStore = defineStore("websocket", () => {
|
|||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
const getWebSocketUrl = async () => {
|
const getWebSocketUrl = async () => {
|
||||||
|
console.log("[WebSocket] Getting WebSocket URL...");
|
||||||
|
|
||||||
// Use environment variable or fallback
|
// Use environment variable or fallback
|
||||||
if (import.meta.env.VITE_WS_URL) {
|
if (import.meta.env.VITE_WS_URL) {
|
||||||
|
console.log(
|
||||||
|
"[WebSocket] Using VITE_WS_URL:",
|
||||||
|
import.meta.env.VITE_WS_URL
|
||||||
|
);
|
||||||
return import.meta.env.VITE_WS_URL;
|
return import.meta.env.VITE_WS_URL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to fetch from backend config API
|
// Try to fetch from backend config API
|
||||||
try {
|
try {
|
||||||
const apiUrl = import.meta.env.VITE_API_URL || "http://localhost:3000";
|
const apiUrl = import.meta.env.VITE_API_URL || "http://localhost:3000";
|
||||||
|
console.log(
|
||||||
|
"[WebSocket] Fetching config from:",
|
||||||
|
`${apiUrl}/api/config/public`
|
||||||
|
);
|
||||||
const response = await fetch(`${apiUrl}/api/config/public`);
|
const response = await fetch(`${apiUrl}/api/config/public`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (data.success && data.config.websocket?.url) {
|
if (data.success && data.config.websocket?.url) {
|
||||||
|
console.log(
|
||||||
|
"[WebSocket] Got URL from backend:",
|
||||||
|
data.config.websocket.url
|
||||||
|
);
|
||||||
return data.config.websocket.url;
|
return data.config.websocket.url;
|
||||||
}
|
}
|
||||||
|
console.warn("[WebSocket] Backend config missing websocket URL");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Failed to fetch WebSocket URL from config:", error);
|
console.warn(
|
||||||
|
"[WebSocket] Failed to fetch WebSocket URL from config:",
|
||||||
|
error
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// In production, use main API domain with /ws path
|
// Fallback: use API domain with /ws path
|
||||||
if (import.meta.env.PROD) {
|
const apiUrl =
|
||||||
const apiUrl =
|
import.meta.env.VITE_API_URL || "https://api.turbotrades.dev";
|
||||||
import.meta.env.VITE_API_URL || "https://api.turbotrades.dev";
|
const fallbackUrl = apiUrl.replace(/^http/, "ws") + "/ws";
|
||||||
// Convert http(s):// to ws(s)://
|
console.log("[WebSocket] Using fallback URL:", fallbackUrl);
|
||||||
return apiUrl.replace(/^http/, "ws") + "/ws";
|
return fallbackUrl;
|
||||||
}
|
|
||||||
|
|
||||||
// In development, use current host with ws path (for proxy)
|
|
||||||
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
|
||||||
const host = window.location.host;
|
|
||||||
return `${protocol}//${host}/ws`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const clearHeartbeat = () => {
|
const clearHeartbeat = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user