UNPKG

video-auth-js-sdk

Version:

A SDK to authenticate users with camera through a realtime stream

78 lines (69 loc) 2.88 kB
import Utility from "./utility/utility" import Async from "podasync-ws-only" import {errorList} from "./errorHandler"; class AsyncManager { constructor(app) { this._app = app; const asyncParams = { appId: app.params.appId, protocol: 'websocket', socketAddress: app.params.socketAddress, serverName: app.params.serverName, deviceId: app.params.deviceId, wsConnectionWaitTime: 10000, reconnectOnClose: false, // asyncLogging: app.sdkParams.asyncLogging, // logLevel: (app.sdkParams.consoleLogging ? 3 : 1), // retryStepTimerTime: protocolManager.getRetryStepTimerTime(), // onStartWithRetryStepGreaterThanZero: onAsyncStateChange, // msgLogCallback: app.publicCallbacks.onDebug || null, // asyncLogCallback: asyncLogCallback || null, onDeviceId: this._onDeviceId.bind(this) }; this._asyncClient = new Async(asyncParams); this._bindListeneres(); // this._requestsList = {} } send(message){ this._asyncClient.send(message); } _onDeviceId(deviceId) { if (!this._localDeviceId) { this._localDeviceId = deviceId; } this._asyncClient.registerDevice(this._localDeviceId); } _bindListeneres() { this._asyncClient.on('asyncReady', function () { this._app.events.emit(this._app.events.eventsList.ASYNC_READY); }.bind(this)); // this._asyncClient.on('stateChange', this._onAsyncMessage); this._asyncClient.on('message', this._onAsyncMessage.bind(this)); this._asyncClient.on('error', function (error) { console.error(error); this._app.publicCallbacks.onError({ code: errorList.CONNECT_TO_SOCKET_FAILED.code, message: errorList.CONNECT_TO_SOCKET_FAILED.message }); }.bind(this)); } _onAsyncMessage(data, ack) { let jsonMessage = (typeof data.content === 'string' && Utility.isValidJson(data.content)) ? JSON.parse(data.content) : data.content; if (data.senderName) { if (data.senderName == this._app.events.eventsList.AI_MESSAGE) { this._app.events.emit(this._app.events.eventsList.AI_MESSAGE, jsonMessage); } } if (jsonMessage && jsonMessage.chatId && jsonMessage.chatId == this._app.authSessionInfo.callId) { this._app.events.emit(this._app.events.eventsList.CALL_MESSAGE, jsonMessage); } this._app.messenger.processResponse(data); ack && ack(); } async destroy(){ await this._asyncClient.logout(); } } export default AsyncManager;