feat(wsClient): Update WebSocket connection to support relative paths and enhance MQTT handling

This commit is contained in:
Tran Anh Tuan
2026-01-27 12:55:01 +07:00
parent 6d1c085ff7
commit ed5751002b
3 changed files with 17 additions and 14 deletions

View File

@@ -9,7 +9,7 @@ class WSClient {
/**
* Kết nối tới WebSocket server.
* @param url Địa chỉ WebSocket server
* @param url Địa chỉ WebSocket server (có thể là relative path như /mqtt)
* @param isAuthenticated Có sử dụng token xác thực hay không
*/
connect(url: string, isAuthenticated: boolean) {
@@ -18,7 +18,13 @@ class WSClient {
if (isAuthenticated) {
token = getToken();
}
const wsUrl = isAuthenticated ? `${url}?token=${token}` : url;
let wsUrl = url;
if (url.startsWith('/')) {
// Relative path, prepend base WebSocket URL
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
wsUrl = `${protocol}//${window.location.host}${url}`;
}
wsUrl = isAuthenticated ? `${wsUrl}?token=${token}` : wsUrl;
this.ws = new ReconnectingWebSocket(wsUrl, [], {
maxRetries: 10,
maxReconnectionDelay: 10000,