UNPKG

@interactify-live/player-react-native

Version:

React Native library for Interactify player with media display, widgets, and MQTT integration

143 lines (142 loc) 5.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Connector = void 0; const Observable_1 = require("./Observable"); const types_1 = require("./types"); // Import precompiled MQTT client const precompiled_mqtt_1 = __importDefault(require("precompiled-mqtt")); // Remove the interface since we're using the real MQTT client class Connector { constructor() { this._status = new Observable_1.Observable(types_1.CONNECTION_STATUS.OFFLINE); this._messages = new Observable_1.Observable([]); this.onMessageReceived = (topic, message) => { try { const parsedMessage = JSON.parse(message.toString()); if (topic === `short/${this.options?.slug}/message/recv`) { const currentMessages = this._messages.get(); this._messages.set([parsedMessage, ...currentMessages]); } } catch (error) { console.error("Failed to parse message:", error); } }; } get status() { return this._status; } get messages() { return this._messages; } getTopics() { if (!this.options) { return []; } return [ `short/${this.options.slug}/analytics/recv`, `short/${this.options.slug}/message/recv`, ]; } connect(options) { if (this.client || this.tempClient) { return; } this.options = options; // Create real MQTT client const clientId = `${options.space_slug}:${options.slug}:${options.user_id}:${options.session}:${options.scope}`; // Use the pure JavaScript MQTT client this.tempClient = precompiled_mqtt_1.default.connect(options.brokerUrl || "wss://messaging.interactify.live/mqtt", { keepalive: 5, clean: true, reconnectPeriod: 1000, connectTimeout: 5 * 1000, clientId: clientId, username: options.user_id, password: options.token, will: { topic: "WillMsg", payload: "Connection Closed abnormally..!", qos: 0, retain: false, }, }); this._status.set(types_1.CONNECTION_STATUS.CONNECTING); this.tempClient.once("connect", () => { this.client = this.tempClient; this.tempClient = undefined; this._status.set(types_1.CONNECTION_STATUS.CONNECTED); // Subscribe to topics const topics = this.getTopics(); topics.forEach((topic) => { this.client.subscribe(topic); }); this.client.on("message", this.onMessageReceived); }); this.tempClient.on("reconnect", () => { this._status.set(types_1.CONNECTION_STATUS.RECONNECTING); }); this.tempClient.on("error", (error) => { console.error("MQTT Error:", error); this._status.set(types_1.CONNECTION_STATUS.ERROR); }); this.tempClient.on("offline", () => { this._status.set(types_1.CONNECTION_STATUS.OFFLINE); }); this.tempClient.on("end", () => { this._status.set(types_1.CONNECTION_STATUS.OFFLINE); }); } disconnect() { if (this.tempClient) { this.tempClient.end(true); this.tempClient = undefined; } if (this.client) { this.client.end(true); this.client = undefined; } this._status.set(types_1.CONNECTION_STATUS.OFFLINE); } publish(type) { if (!this.client || !this.options) { throw new Error("not connected"); } this.client.publish(`${this.options.scope}/${this.options.slug}/analytics/send`, JSON.stringify({ event_type: "ACTION_CLICK", value: { event_type: type, user_id: this.options.user_id, }, })); } sendMessage(message) { if (!this.client || !this.options) { throw new Error("not connected"); } const chatMessage = { avatar: message.avatar || "", text: message.text, created_at: new Date().toISOString(), user_id: this.options.user_id, display_name: message.displayName || "", live_slug: "", space_slug: this.options.space_slug, visible: message.visible !== false, slug: this.options.slug, reply_to: message.replyTo ? { text: message.replyTo.text, user_id: message.replyTo.userID, display_name: message.replyTo.displayName, } : undefined, }; this.client.publish(`short/${this.options.slug}/message/send`, JSON.stringify(chatMessage)); } } exports.Connector = Connector; Connector.CONNECTION_STATUS = types_1.CONNECTION_STATUS;