feat(wsClient): Update WebSocket connection to support relative paths and enhance MQTT handling
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user