UNPKG

@bxshefby/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

136 lines (133 loc) • 4.07 kB
/** * @package @bxshefby/b24jssdk * @version 3.3.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bx-shef/b24-js-sdk * @see https://b24-js-sdk.bx-shef.by/ */ import { getDateForLog } from '../tools/text-core.mjs'; import { AbstractConnector } from './abstract-connector.mjs'; import { ConnectionType } from '../types/pull.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const CONNECTING_TIMEOUT_MS = 3e4; class WebSocketConnector extends AbstractConnector { static { __name(this, "WebSocketConnector"); } _socket; // When the current socket was created: a socket stuck in CONNECTING longer than // CONNECTING_TIMEOUT_MS is replaced on the next connect() (#61). _socketCreatedAt = 0; _onSocketOpenHandler; _onSocketCloseHandler; _onSocketErrorHandler; _onSocketMessageHandler; constructor(config) { super(config); this._connectionType = ConnectionType.WebSocket; this._socket = null; this._onSocketOpenHandler = this._onSocketOpen.bind(this); this._onSocketCloseHandler = this._onSocketClose.bind(this); this._onSocketErrorHandler = this._onSocketError.bind(this); this._onSocketMessageHandler = this._onSocketMessage.bind(this); } destroy() { super.destroy(); if (this._socket) { this._socket.close(); this._socket = null; } } /** * @inheritDoc */ connect() { if (this._socket) { const opening = this._socket.readyState === 0 && Date.now() - this._socketCreatedAt < CONNECTING_TIMEOUT_MS; if (this._socket.readyState === 1 || opening) { return; } else { this.clearEventListener(); this._socket.close(); this._socket = null; } } this._createSocket(); } get socket() { return this._socket; } /** * @inheritDoc * @param code * @param reason */ disconnect(code, reason) { if (this._socket !== null) { this.clearEventListener(); this._socket.close(code, reason); } this._socket = null; this._disconnectCode = code; this._disconnectReason = reason; this.connected = false; } /** * Via websocket connection * @inheritDoc */ send(buffer) { if (!this._socket || this._socket.readyState !== 1) { this.getLogger().error(`${getDateForLog()}: Pull: WebSocket is not connected`).catch(() => { }); return false; } this._socket.send(buffer); return true; } // region Event Handlers //// _onSocketOpen() { this.connected = true; } _onSocketClose(event) { this._socket = null; this._disconnectCode = Number(event.code); this._disconnectReason = event.reason; this.connected = false; } _onSocketError(event) { this._callbacks.onError(new Error(`Socket error: ${event}`)); } _onSocketMessage(event) { this._callbacks.onMessage(event.data); } // endregion //// // region Tools //// clearEventListener() { if (this._socket) { this._socket.removeEventListener("open", this._onSocketOpenHandler); this._socket.removeEventListener("close", this._onSocketCloseHandler); this._socket.removeEventListener("error", this._onSocketErrorHandler); this._socket.removeEventListener("message", this._onSocketMessageHandler); } } _createSocket() { if (this._socket) { throw new Error("Socket already exists"); } if (!this.connectionPath) { throw new Error("Websocket connection path is not defined"); } this._socket = new WebSocket(this.connectionPath); this._socketCreatedAt = Date.now(); this._socket.binaryType = "arraybuffer"; this._socket.addEventListener("open", this._onSocketOpenHandler); this._socket.addEventListener("close", this._onSocketCloseHandler); this._socket.addEventListener("error", this._onSocketErrorHandler); this._socket.addEventListener("message", this._onSocketMessageHandler); } // endregion //// } export { WebSocketConnector };