UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

130 lines (127 loc) 3.71 kB
/** * @package @bitrix24/b24jssdk * @version 1.0.1 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { Text } from '../tools/text.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 }); class WebSocketConnector extends AbstractConnector { static { __name(this, "WebSocketConnector"); } _socket; _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) { if (this._socket.readyState === 1) { 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(`${Text.getDateForLog()}: Pull: WebSocket is not connected`); 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._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 }; //# sourceMappingURL=web-socket-connector.mjs.map