UNPKG

node-nim

Version:

NetEase IM nodejs wrapper based on NetEase IM C++ SDK

256 lines 8.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.V2NIMChatroomClient = void 0; const loader_1 = __importDefault(require("../loader")); const eventemitter3_1 = require("eventemitter3"); const v2_nim_chatroom_service_1 = require("./v2_nim_chatroom_service"); const v2_nim_storage_service_1 = require("./v2_nim_storage_service"); const v2_nim_chatroom_queue_service_1 = require("./v2_nim_chatroom_queue_service"); class V2NIMChatroomClient extends eventemitter3_1.EventEmitter { constructor(instanceId) { super(); this.instance = new loader_1.default.V2NIMChatroomClient({ emit: this.emit.bind(this) }, instanceId); this.chatroomService = new v2_nim_chatroom_service_1.V2NIMChatroomService(this.instance.getInstanceId()); this.chatroomQueueService = new v2_nim_chatroom_queue_service_1.V2NIMChatroomQueueService(this.instance.getInstanceId()); this.storageService = new v2_nim_storage_service_1.V2NIMStorageService(this.instance.getInstanceId()); } /** * @brief 初始化 * @param option 初始化选项 * @returns V2NIMError * @example * ```javascript * const result = await V2NIMChatroomClient.init({ * appKey: 'your app key' * }) * if (result) { * console.error(result) * } * ``` */ static init(option) { return V2NIMChatroomClient.chatroomSdk.init(option); } /** * @brief 反初始化 * @returns void * @warning 请在退出程序前调用此方法 * @example * ```javascript * V2NIMChatroomClient.uninit() * ``` */ static uninit() { return V2NIMChatroomClient.chatroomSdk.uninit(); } /** * @brief 创建聊天室客户端实例 * @returns V2NIMChatroomClient * @pre 必须在调用此方法前调用 V2NIMChatroomClient::init * @note 建议保存持有实例 ID 而不是实例本身 * @example * ```javascript * const chatroomClient = V2NIMChatroomClient.newInstance() * ``` */ static newInstance() { return new V2NIMChatroomClient(); } /** * @brief 销毁聊天室客户端实例 * @param instanceId 聊天室客户端实例 ID * @returns void * @pre 必须在调用此方法前调用 V2NIMChatroomClient::init * @warning 严禁在调用此方法后访问对应的聊天室客户端实例 * @example * ```javascript * V2NIMChatroomClient.destroyInstance(instanceId) * ``` */ static destroyInstance(instanceId) { return V2NIMChatroomClient.chatroomSdk.destroyInstance(instanceId); } /** * @param instanceId 聊天室客户端实例 ID * @returns V2NIMChatroomClient * @pre 必须在调用此方法前调用 V2NIMChatroomClient::init * @example * ```javascript * const chatroomClient = V2NIMChatroomClient.getInstance(instanceId) * ``` */ static getInstance(instanceId) { // check instanceId should be number if (typeof instanceId !== 'number') { return null; } return new V2NIMChatroomClient(instanceId); } /** * @brief 获取聊天室实例列表 * @returns Array<V2NIMChatroomClient> * @pre 必须在调用此方法前调用 V2NIMChatroomClient::init * @example * ```javascript * const instanceList = V2NIMChatroomClient.getInstanceList() * ``` */ static getInstanceList() { const instanceList = V2NIMChatroomClient.chatroomSdk.getInstanceIdList(); const result = []; for (const instanceId of instanceList) { result.push(new V2NIMChatroomClient(instanceId)); } return result; } /** * @brief 销毁所有聊天室客户端实例 * @returns void * @pre 必须在调用此方法前调用 V2NIMChatroomClient::init * @warning 严禁在调用此方法后访问任何聊天室客户端实例, 此方法会退出聊天室, 耗时可能较长 * @example * ```javascript * V2NIMChatroomClient.destroyAll() * ``` */ static destroyAll() { return V2NIMChatroomClient.chatroomSdk.destroyAll(); } /** * @brief 获取聊天室客户端实例 ID * @returns size_t * @example * ```javascript * const instanceId = chatroomClient.getInstanceId() * ``` */ getInstanceId() { return this.instance.getInstanceId(); } /** * @brief 更新 appKey * @param appKey - 新的 App key * @returns V2NIMError | null * @example * ```javascript * const result = chatroomClient.updateAppKey('your new app key') * if (result) { * console.error(result) * } * ``` */ updateAppKey(appKey) { return this.instance.updateAppKey(appKey); } /** * @brief 进入聊天室 * @param roomId 聊天室 ID * @param enterParams 进入聊天室相关参数 * @returns Promise<V2NIMChatroomEnterResult> * @example * ```javascript * const result = await chatroomClient.enter('your room id', { * accountId: 'your account id', * token: 'your token', * roomNick: 'your room nick', * linkProvider: (roomId, account) => { * return ['chatroom link...'] * } * }) * if (result) { * console.error(result) * } * ``` */ enter(roomId, enterParams) { if (enterParams.__linkProvider) { enterParams.linkProvider = (account, roomId) => { return enterParams.__linkProvider; }; } if (enterParams.loginOption && enterParams.loginOption.__tokenProvider) { enterParams.loginOption.tokenProvider = (account, roomId) => { if (!enterParams.loginOption) { return ''; } return enterParams.loginOption.__tokenProvider; }; } if (enterParams.loginOption && enterParams.loginOption.__loginExtensionProvider) { enterParams.loginOption.loginExtensionProvider = (account, roomId) => { if (!enterParams.loginOption) { return ''; } return enterParams.loginOption.__loginExtensionProvider; }; } return new Promise((resolve, reject) => { this.instance.enter(roomId, enterParams, (result) => { resolve(result); }, (error) => { reject(error); }); }); } /** * @brief 退出聊天室 * @returns void * @example * ```javascript * chatroomClient.exit() * ``` */ exit() { this.instance.exit(); } /** * @brief 查询聊天室信息 * @returns V2NIMChatroomInfo * @example * ```javascript * const chatroomInfo = chatroomClient.getChatroomInfo() * ``` */ getChatroomInfo() { return this.instance.getChatroomInfo(); } /** * @brief 获取聊天室服务 * @returns V2NIMChatroomService * @example * ```javascript * const chatroomService = chatroomClient.getChatroomService() * ``` */ getChatroomService() { return this.chatroomService; } /** * @brief 获取聊天室队列服务 * @returns V2NIMChatroomQueueService * @example * ```javascript * const chatroomQueueService = chatroomClient.getChatroomQueueService() * ``` */ getChatroomQueueService() { return this.chatroomQueueService; } /** * @brief 获取存储服务 * @returns V2NIMStorageService * @example * ```javascript * const storageService = chatroomClient.getStorageService() * ``` */ getStorageService() { return this.storageService; } } exports.V2NIMChatroomClient = V2NIMChatroomClient; V2NIMChatroomClient.chatroomSdk = new loader_1.default.V2NIMChatroomSdk(); //# sourceMappingURL=v2_nim_chatroom_client.js.map