##update fix session dan tampilan screen
This commit is contained in:
+29
-11
@@ -10,6 +10,7 @@ export interface WebSocketConfig {
|
||||
onError?: (error: Event) => void
|
||||
reconnectInterval?: number
|
||||
maxReconnectAttempts?: number
|
||||
fallbackPostUrl?: string // Optional: use a specific URL for the POST fallback (e.g., a proxy)
|
||||
}
|
||||
|
||||
export interface WebSocketMessage {
|
||||
@@ -24,7 +25,18 @@ export const useWebSocket = (config: WebSocketConfig) => {
|
||||
const reconnectTimer = ref<NodeJS.Timeout | null>(null)
|
||||
|
||||
const wsUrl = computed(() => {
|
||||
const url = new URL(config.url)
|
||||
let baseUrl = config.url
|
||||
|
||||
// Automatically upgrade to secure protocols if caller uses HTTPS
|
||||
if (typeof window !== 'undefined' && window.location.protocol === 'https:') {
|
||||
if (baseUrl.startsWith('ws://')) {
|
||||
baseUrl = baseUrl.replace('ws://', 'wss://')
|
||||
} else if (baseUrl.startsWith('http://')) {
|
||||
baseUrl = baseUrl.replace('http://', 'https://')
|
||||
}
|
||||
}
|
||||
|
||||
const url = new URL(baseUrl)
|
||||
url.searchParams.set('client_id', config.clientId)
|
||||
return url.toString()
|
||||
})
|
||||
@@ -106,17 +118,23 @@ export const useWebSocket = (config: WebSocketConfig) => {
|
||||
// Send data via POST to WebSocket server
|
||||
const sendViaPost = async (message: WebSocketMessage) => {
|
||||
try {
|
||||
// Extract base URL from WebSocket URL (convert ws:// to http:// or wss:// to https://)
|
||||
let baseUrl = config.url
|
||||
if (baseUrl.startsWith('ws://')) {
|
||||
baseUrl = baseUrl.replace('ws://', 'http://')
|
||||
} else if (baseUrl.startsWith('wss://')) {
|
||||
baseUrl = baseUrl.replace('wss://', 'https://')
|
||||
let postUrl = ''
|
||||
|
||||
if (config.fallbackPostUrl) {
|
||||
// Use the explicitly provided fallback URL (likely a relative proxy path)
|
||||
postUrl = config.fallbackPostUrl
|
||||
} else {
|
||||
// Derive post URL from WebSocket URL (legacy behavior)
|
||||
let baseUrl = config.url
|
||||
if (baseUrl.startsWith('ws://')) {
|
||||
baseUrl = baseUrl.replace('ws://', 'http://')
|
||||
} else if (baseUrl.startsWith('wss://')) {
|
||||
baseUrl = baseUrl.replace('wss://', 'https://')
|
||||
}
|
||||
|
||||
const urlObj = new URL(baseUrl)
|
||||
postUrl = `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}`
|
||||
}
|
||||
|
||||
// Remove query parameters and ensure we have the correct POST endpoint
|
||||
const urlObj = new URL(baseUrl)
|
||||
const postUrl = `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}`
|
||||
|
||||
console.log('📡 POST URL:', postUrl)
|
||||
console.log('📦 POST Body:', JSON.stringify(message, null, 2))
|
||||
|
||||
Reference in New Issue
Block a user