102 lines
2.9 KiB
JavaScript
102 lines
2.9 KiB
JavaScript
const DEFAULT_TIMEOUT_MS = 12000;
|
||
const runtimeTimers = globalThis;
|
||
|
||
function buildWsUrl(raw) {
|
||
const value = String(raw || '').trim();
|
||
if (!value) return 'wss://shineup.me/ws';
|
||
if (value.startsWith('ws://') || value.startsWith('wss://')) return value;
|
||
if (value.startsWith('http://') || value.startsWith('https://')) {
|
||
const parsed = new URL(value);
|
||
parsed.protocol = parsed.protocol === 'https:' ? 'wss:' : 'ws:';
|
||
if (!parsed.pathname || parsed.pathname === '/') parsed.pathname = '/ws';
|
||
return parsed.toString();
|
||
}
|
||
return value;
|
||
}
|
||
|
||
function createRequestId(op) {
|
||
return `${op}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||
}
|
||
|
||
export class WsJsonClient {
|
||
constructor(url) {
|
||
this.url = buildWsUrl(url);
|
||
this.ws = null;
|
||
this.openPromise = null;
|
||
this.pending = new Map();
|
||
}
|
||
|
||
async open() {
|
||
if (this.ws && this.ws.readyState === WebSocket.OPEN) return;
|
||
if (this.openPromise) return this.openPromise;
|
||
|
||
this.openPromise = new Promise((resolve, reject) => {
|
||
const ws = new WebSocket(this.url);
|
||
this.ws = ws;
|
||
|
||
ws.addEventListener('open', () => resolve(), { once: true });
|
||
ws.addEventListener('error', () => reject(new Error(`Не удалось подключиться к ${this.url}`)), { once: true });
|
||
ws.addEventListener('close', () => this.failPending('WebSocket соединение закрыто'));
|
||
ws.addEventListener('message', (event) => this.handleMessage(event.data));
|
||
}).finally(() => {
|
||
this.openPromise = null;
|
||
});
|
||
|
||
return this.openPromise;
|
||
}
|
||
|
||
async request(op, payload = {}, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
||
await this.open();
|
||
const requestId = createRequestId(op);
|
||
const body = { op, requestId, payload };
|
||
|
||
const response = new Promise((resolve, reject) => {
|
||
const timer = runtimeTimers.setTimeout(() => {
|
||
this.pending.delete(requestId);
|
||
reject(new Error(`Таймаут ответа для операции ${op}`));
|
||
}, timeoutMs);
|
||
this.pending.set(requestId, {
|
||
resolve: (value) => {
|
||
runtimeTimers.clearTimeout(timer);
|
||
resolve(value);
|
||
},
|
||
reject: (error) => {
|
||
runtimeTimers.clearTimeout(timer);
|
||
reject(error);
|
||
},
|
||
});
|
||
});
|
||
|
||
this.ws.send(JSON.stringify(body));
|
||
return response;
|
||
}
|
||
|
||
handleMessage(raw) {
|
||
let data;
|
||
try {
|
||
data = JSON.parse(raw);
|
||
} catch {
|
||
return;
|
||
}
|
||
const requestId = data?.requestId;
|
||
if (!requestId) return;
|
||
const slot = this.pending.get(requestId);
|
||
if (!slot) return;
|
||
this.pending.delete(requestId);
|
||
slot.resolve(data);
|
||
}
|
||
|
||
failPending(message) {
|
||
const error = new Error(message);
|
||
for (const slot of this.pending.values()) slot.reject(error);
|
||
this.pending.clear();
|
||
}
|
||
|
||
close() {
|
||
if (this.ws) {
|
||
this.ws.close();
|
||
this.ws = null;
|
||
}
|
||
}
|
||
}
|