UNPKG

rhine-var

Version:

Variables that support multi-user collaboration and persistence, making collaboration and variable operations as simple as possible, with strict and well-defined type hints.

75 lines (74 loc) 3.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const y_websocket_1 = require("y-websocket"); const config_1 = __importDefault(require("../../../config/config")); const connector_status_enum_1 = require("../connector-status.enum"); const connector_abstract_1 = __importDefault(require("../connector.abstract")); const sync_handshake_check_class_1 = __importDefault(require("./sync-handshake-check.class")); const index_1 = require("../../../index"); const logger_1 = require("../../../utils/logger"); class WebsocketConnector extends connector_abstract_1.default { constructor() { super(...arguments); this.name = ''; this.url = ''; this.provider = null; } async connect(text) { const li = this.url.lastIndexOf('/'); if (li == -1 || li == this.url.length - 1 || !this.url.startsWith('ws')) { (0, logger_1.error)('WebsocketConnector: UnSupport URL to connect room'); return; } this.name = text.substring(li + 1); this.url = text.substring(0, li); this.yDoc = new index_1.YDoc(); this.yBaseMap = this.yDoc.getMap(); return new Promise((resolve, reject) => { this.provider = new y_websocket_1.WebsocketProvider(this.url, this.name, this.yDoc); this.provider.shouldConnect = true; this.provider.on('status', (event) => { this.status = event.status; (0, logger_1.log)('WebsocketConnector.event status:', event.status); }); this.provider.on('sync', async (synced) => { if (synced) { if (config_1.default.ENABLE_SYNC_HANDSHAKE_CHECK) { await sync_handshake_check_class_1.default.wait(this.yBaseMap); } (0, logger_1.log)('WebsocketConnector.event sync:', this.yBaseMap.toJSON()); this.synced = true; this.clientId = this.yDoc.clientID; this.emitSynced(synced); resolve(); } else { (0, logger_1.log)('WebsocketConnector.event sync:', false); } }); this.provider.on('connection-close', (e) => { this.status = connector_status_enum_1.ConnectorStatus.DISCONNECTED; (0, logger_1.log)('WebsocketConnector.event connection-close:', e); }); this.provider.on('connection-error', (error) => { this.status = connector_status_enum_1.ConnectorStatus.DISCONNECTED; (0, logger_1.log)('WebsocketConnector.event connection-error:', error); }); }); } async disconnect() { throw new Error('Disconnecting and switching connections are not supported at the moment.'); /* if (this.provider) { this.provider.disconnect() } this.provider = null this.synced = false this.websocketStatus = ConnectorStatus.DISCONNECTED */ } } exports.default = WebsocketConnector;