UNPKG

fcr-core

Version:

Core APIs for building online scenes

304 lines (300 loc) 18.6 kB
"use strict"; require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.array.push.js"); require("core-js/modules/esnext.function.metadata.js"); require("core-js/modules/esnext.iterator.filter.js"); require("core-js/modules/esnext.iterator.for-each.js"); require("core-js/modules/esnext.symbol.metadata.js"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.FcrRoomSessionControlImpl = void 0; require("core-js/modules/es.error.cause.js"); require("core-js/modules/es.json.stringify.js"); require("core-js/modules/esnext.iterator.constructor.js"); require("core-js/modules/esnext.iterator.map.js"); require("core-js/modules/esnext.map.delete-all.js"); require("core-js/modules/esnext.map.emplace.js"); require("core-js/modules/esnext.map.every.js"); require("core-js/modules/esnext.map.filter.js"); require("core-js/modules/esnext.map.find.js"); require("core-js/modules/esnext.map.find-key.js"); require("core-js/modules/esnext.map.includes.js"); require("core-js/modules/esnext.map.key-of.js"); require("core-js/modules/esnext.map.map-keys.js"); require("core-js/modules/esnext.map.map-values.js"); require("core-js/modules/esnext.map.merge.js"); require("core-js/modules/esnext.map.reduce.js"); require("core-js/modules/esnext.map.some.js"); require("core-js/modules/esnext.map.update.js"); require("core-js/modules/web.dom-collections.iterator.js"); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _imports = require("../../imports"); var _type = require("../../type"); var _error = require("../../utilities/error"); var _logger = require("../../utilities/logger"); var _validateParams = _interopRequireDefault(require("../../utilities/validate-params")); var _schema = require("../../schema"); var _helpers = require("../helpers"); var _FcrRoomSessionControlImpl; let _initProto, _startRoomSessionDecs, _stopRoomSessionDecs, _acceptRoomSessionDecs, _rejectRoomSessionDecs, _ref; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _applyDecs(e, t, r, n, o, a) { function i(e, t, r) { return function (n, o) { return r && r(n), e[t].call(n, o); }; } function c(e, t) { for (var r = 0; r < e.length; r++) e[r].call(t); return t; } function s(e, t, r, n) { if ("function" != typeof e && (n || void 0 !== e)) throw new TypeError(t + " must " + (r || "be") + " a function" + (n ? "" : " or undefined")); return e; } function applyDec(e, t, r, n, o, a, c, u, l, f, p, d, h) { function m(e) { if (!h(e)) throw new TypeError("Attempted to access private element on non-instance"); } var y, v = t[0], g = t[3], b = !u; if (!b) { r || Array.isArray(v) || (v = [v]); var w = {}, S = [], A = 3 === o ? "get" : 4 === o || d ? "set" : "value"; f ? (p || d ? w = { get: _setFunctionName(function () { return g(this); }, n, "get"), set: function (e) { t[4](this, e); } } : w[A] = g, p || _setFunctionName(w[A], n, 2 === o ? "" : A)) : p || (w = Object.getOwnPropertyDescriptor(e, n)); } for (var P = e, j = v.length - 1; j >= 0; j -= r ? 2 : 1) { var D = v[j], E = r ? v[j - 1] : void 0, I = {}, O = { kind: ["field", "accessor", "method", "getter", "setter", "class"][o], name: n, metadata: a, addInitializer: function (e, t) { if (e.v) throw Error("attempted to call addInitializer after decoration was finished"); s(t, "An initializer", "be", !0), c.push(t); }.bind(null, I) }; try { if (b) (y = s(D.call(E, P, O), "class decorators", "return")) && (P = y);else { var k, F; O.static = l, O.private = f, f ? 2 === o ? k = function (e) { return m(e), w.value; } : (o < 4 && (k = i(w, "get", m)), 3 !== o && (F = i(w, "set", m))) : (k = function (e) { return e[n]; }, (o < 2 || 4 === o) && (F = function (e, t) { e[n] = t; })); var N = O.access = { has: f ? h.bind() : function (e) { return n in e; } }; if (k && (N.get = k), F && (N.set = F), P = D.call(E, d ? { get: w.get, set: w.set } : w[A], O), d) { if ("object" == typeof P && P) (y = s(P.get, "accessor.get")) && (w.get = y), (y = s(P.set, "accessor.set")) && (w.set = y), (y = s(P.init, "accessor.init")) && S.push(y);else if (void 0 !== P) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0"); } else s(P, (p ? "field" : "method") + " decorators", "return") && (p ? S.push(P) : w[A] = P); } } finally { I.v = !0; } } return (p || d) && u.push(function (e, t) { for (var r = S.length - 1; r >= 0; r--) t = S[r].call(e, t); return t; }), p || b || (f ? d ? u.push(i(w, "get"), i(w, "set")) : u.push(2 === o ? w[A] : i.call.bind(w[A])) : Object.defineProperty(e, n, w)), P; } function u(e, t) { return Object.defineProperty(e, Symbol.metadata || Symbol.for("Symbol.metadata"), { configurable: !0, enumerable: !0, value: t }); } if (arguments.length >= 6) var l = a[Symbol.metadata || Symbol.for("Symbol.metadata")]; var f = Object.create(null == l ? null : l), p = function (e, t, r, n) { var o, a, i = [], s = function (t) { return _checkInRHS(t) === e; }, u = new Map(); function l(e) { e && i.push(c.bind(null, e)); } for (var f = 0; f < t.length; f++) { var p = t[f]; if (Array.isArray(p)) { var d = p[1], h = p[2], m = p.length > 3, y = 16 & d, v = !!(8 & d), g = 0 == (d &= 7), b = h + "/" + v; if (!g && !m) { var w = u.get(b); if (!0 === w || 3 === w && 4 !== d || 4 === w && 3 !== d) throw Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + h); u.set(b, !(d > 2) || d); } applyDec(v ? e : e.prototype, p, y, m ? "#" + h : _toPropertyKey(h), d, n, v ? a = a || [] : o = o || [], i, v, m, g, 1 === d, v && m ? s : r); } } return l(o), l(a), i; }(e, t, o, f); return r.length || u(e, f), { e: p, get c() { var t = []; return r.length && [u(applyDec(e, [r], n, e.name, 5, f, t), f), c.bind(null, t, e)]; } }; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _setFunctionName(e, t, n) { "symbol" == typeof t && (t = (t = t.description) ? "[" + t + "]" : ""); try { Object.defineProperty(e, "name", { configurable: !0, value: n ? n + " " + t : t }); } catch (e) {} return e; } function _checkInRHS(e) { if (Object(e) !== e) throw TypeError("right-hand side of 'in' should be an object, got " + (null !== e ? typeof e : "null")); return e; } /** * @internal */ _ref = (_startRoomSessionDecs = [_imports.trace, (0, _validateParams.default)(_schema.fcrRoomSessionParamsSchema, _schema.fcrPrivilegeUserRoleArraySchema.optional())], _stopRoomSessionDecs = [_imports.trace, (0, _validateParams.default)(_schema.stringSchema)], _acceptRoomSessionDecs = [_imports.trace, (0, _validateParams.default)(_schema.fcrSessionBaseSchema, _schema.stringKeyUnknownValueSchema.optional())], _rejectRoomSessionDecs = [_imports.trace, (0, _validateParams.default)(_schema.fcrSessionBaseSchema, _schema.stringKeyUnknownValueSchema.optional())], "logger"); class FcrRoomSessionControlImpl { constructor(_roomControl, _api, _userId) { //@internal (0, _defineProperty2.default)(this, _ref, (_initProto(this), (0, _logger.createLogger)({ prefix: 'FcrRoomSessionControlImpl' }))); //@internal (0, _defineProperty2.default)(this, "_observable", new _imports.AgoraObservable()); //@internal (0, _defineProperty2.default)(this, "_taskInterval", _imports.Duration.second(1)); //@internal (0, _defineProperty2.default)(this, "_scheduleMap", new Map()); //@internal (0, _defineProperty2.default)(this, "_receivedMap", new Map()); this._roomControl = _roomControl; this._api = _api; this._userId = _userId; this._executeTask = this._executeTask.bind(this); this._addLogObserver(); this._roomControl.addObserver({ onRoomMessageReceived: this._handleRoomMessage.bind(this) }); this._task = _imports.AgoraScheduler.shared.addPollingTask(this._executeTask, this._taskInterval, true, () => _imports.ActionWhenTaskFail.CONTINUE); } async startRoomSession(params, targetRoles) { try { const session = { sessionId: (0, _imports.md5)("".concat(this._userId).concat(Date.now())), sessionKey: params.sessionKey, payload: params.payload, duration: params.duration, sender: this._userId, targetRoles: targetRoles ? targetRoles.map(role => _type.FcrPrivilegeUserRoleToStringMap[role]) : undefined }; const { data: { interval } } = await this._api.updateRoomSession({ userId: this._userId, sessionId: session.sessionId, sessionKey: session.sessionKey, duration: session.duration, payload: session.payload, targetId: this._roomControl.getRoomInfo().roomId, targetRoles: session.targetRoles }); const nextTs = Date.now() + interval; this._scheduleMap.set(session.sessionId, _objectSpread(_objectSpread({}, params), {}, { targetRoles: session.targetRoles, nextTs, startTs: Date.now() })); this.logger.info("[RoomSession] put into session schedule: ".concat(session.sessionId, ", next ts: ").concat(nextTs, ", remain: ").concat(nextTs - Date.now(), "ms")); return session.sessionId; } catch (e) { throw (0, _error.generateFcrCoreServerError)(_error.FcrErrorCode.LOCAL_HTTP_REQUEST_FAILED, 'start room session failed', e); } } stopRoomSession(sessionId) { this._scheduleMap.delete(sessionId); } async acceptRoomSession(session, cause) { if (this._isSessionExpired(session)) { throw (0, _error.generateFcrCoreServerError)(_error.FcrErrorCode.EXPIRED, 'accept room session failed: session expired', new Error()); } try { this._api.deleteRoomSession({ userId: this._userId, sessionId: session.sessionId, sessionKey: session.sessionKey, receiverIds: [session.senderId], target: 2, action: 1, payload: { cause } }); } catch (e) { throw (0, _error.generateFcrCoreServerError)(_error.FcrErrorCode.LOCAL_HTTP_REQUEST_FAILED, 'accept room session failed', e); } } async rejectRoomSession(session, cause) { if (this._isSessionExpired(session)) { throw (0, _error.generateFcrCoreServerError)(_error.FcrErrorCode.EXPIRED, 'reject room session failed: session expired', new Error()); } try { this._api.deleteRoomSession({ userId: this._userId, sessionId: session.sessionId, sessionKey: session.sessionKey, receiverIds: [session.senderId], target: 2, action: 2, payload: { cause } }); } catch (e) { throw (0, _error.generateFcrCoreServerError)(_error.FcrErrorCode.LOCAL_HTTP_REQUEST_FAILED, 'reject room session failed', e); } } addObserver(observer) { this._observable.addObserver(observer); } removeObserver(observer) { this._observable.removeObserver(observer); } //@internal async _executeTask() { // ping sessions (sender side) for (const key of this._scheduleMap.keys()) { const params = this._scheduleMap.get(key); const session = { sessionId: key, sessionKey: params.sessionKey, payload: params.payload, duration: params.duration, senderId: this._userId, timestamp: params.startTs, targetRoles: params.targetRoles, interval: 0 }; if (this._isSessionExpired(session)) { this.logger.info("[RoomSession] room session expired, removed: ".concat(session.sessionId)); this._scheduleMap.delete(session.sessionId); continue; } // check timeout if (params.nextTs < Date.now()) { this.logger.info("[RoomSession] ping session: ".concat(session.sessionId)); try { const { data: { interval } } = await this._api.updateRoomSession({ userId: this._userId, sessionId: session.sessionId, sessionKey: session.sessionKey, duration: params.duration, payload: session.payload, targetRoles: session.targetRoles, targetId: this._roomControl.getRoomInfo().roomId }); const nextTs = Date.now() + interval; if (this._scheduleMap.has(session.sessionId)) { this._scheduleMap.set(session.sessionId, _objectSpread(_objectSpread({}, params), {}, { nextTs })); this.logger.info("[RoomSession] refresh session schedule: ".concat(session.sessionId, ", next ts: ").concat(nextTs)); } } catch (e) { this.logger.error("[RoomSession] ping session failed: ".concat(e)); } } } // clear expired sessions (receiver side) for (const key of this._receivedMap.keys()) { const session = this._receivedMap.get(key); if (this._isSessionExpired(session)) { this._receivedMap.delete(key); this.logger.info("[RoomSession] delete session callback: ".concat(key)); } } } //@internal async _handleRoomMessage(roomId, message) { const { senderId, payload, cmd } = message; const data = payload; if (cmd === _helpers.ROOM_MESSAGE_COMMANDS.ROOM_SESSION_REQUEST) { const session = { sessionId: data.sessionUuid, sessionKey: data.sessionKey, senderId, duration: data.duration, timestamp: message.timestamp, interval: data.interval, payload: data.payload }; const sessionCache = this._receivedMap.get(session.sessionId); if (sessionCache) { session.timestamp = sessionCache.timestamp; } if (!this._isSessionExpired(session)) { if (!this._receivedMap.has(session.sessionId)) { this.logger.info("[RoomSession] received room session request ".concat(JSON.stringify(session))); this._observable.notifyObservers('onRoomSessionReceived', roomId, session); } this._receivedMap.set(session.sessionId, session); } } else if (cmd === _helpers.ROOM_MESSAGE_COMMANDS.ROOM_SESSION_ACCEPT) { // TODO: BUG FIXSME: onRoomSessionAccepted 和 onRoomSessionRejected 两个事件, apaas private doc 描述的是监听 AgoraRteEngine.onSceneMessageReceived // 但是点对点消息实际上触发的是 AgoraRteEngine.onPeerMessageReceived 事件. 所以这里并不会收到 cmd === ROOM_SESSION_ACCEPT 和 ROOM_SESSION_REJECT 的消息. if (data.action === 1) { if (this._scheduleMap.has(data.sessionUuid)) { var _session$payload, _ref2, _data$payload; const session = this._scheduleMap.get(data.sessionUuid); const sessionResponse = { sessionId: data.sessionUuid, sessionKey: data.sessionKey, responderId: senderId, // FIXME: session may be lost when the client refreshes // get payload from original session payload: (_session$payload = session.payload) !== null && _session$payload !== void 0 ? _session$payload : {}, cause: (_ref2 = (_data$payload = data.payload) === null || _data$payload === void 0 ? void 0 : _data$payload.cause) !== null && _ref2 !== void 0 ? _ref2 : {} }; this.logger.info("[RoomSession] received room session accepted ".concat(JSON.stringify(sessionResponse))); this._scheduleMap.delete(data.sessionUuid); this._observable.notifyObservers('onRoomSessionAccepted', roomId, sessionResponse); } } else if (data.action === 2) { if (this._scheduleMap.has(data.sessionUuid)) { var _session$payload2, _ref3, _data$payload2; const session = this._scheduleMap.get(data.sessionUuid); const sessionResponse = { sessionId: data.sessionUuid, sessionKey: data.sessionKey, responderId: senderId, // FIXME: session may be lost when the client refreshes // get payload from original session payload: (_session$payload2 = session.payload) !== null && _session$payload2 !== void 0 ? _session$payload2 : {}, cause: (_ref3 = (_data$payload2 = data.payload) === null || _data$payload2 === void 0 ? void 0 : _data$payload2.cause) !== null && _ref3 !== void 0 ? _ref3 : {} }; this.logger.info("[RoomSession] received room session rejected ".concat(JSON.stringify(sessionResponse))); if (this._scheduleMap.has(data.sessionUuid)) { this._scheduleMap.delete(data.sessionUuid); this._observable.notifyObservers('onRoomSessionRejected', roomId, sessionResponse); } } } } } //@internal _isSessionExpired(session) { return session.timestamp + session.duration * _imports.Duration.second(1) < Date.now(); } _addLogObserver() { this.addObserver((0, _logger.generateLogObserver)(this.logger, ['onRoomSessionAccepted', 'onRoomSessionRejected', 'onRoomSessionReceived'])); } } exports.FcrRoomSessionControlImpl = FcrRoomSessionControlImpl; _FcrRoomSessionControlImpl = FcrRoomSessionControlImpl; [_initProto] = _applyDecs(_FcrRoomSessionControlImpl, [[_startRoomSessionDecs, 2, "startRoomSession"], [_stopRoomSessionDecs, 2, "stopRoomSession"], [_acceptRoomSessionDecs, 2, "acceptRoomSession"], [_rejectRoomSessionDecs, 2, "rejectRoomSession"]], []).e;