UNPKG

fcr-core

Version:

Core APIs for building online scenes

702 lines (694 loc) 32.8 kB
import "core-js/modules/es.array.push.js"; import "core-js/modules/esnext.function.metadata.js"; import "core-js/modules/esnext.map.delete-all.js"; import "core-js/modules/esnext.map.emplace.js"; import "core-js/modules/esnext.map.every.js"; import "core-js/modules/esnext.map.filter.js"; import "core-js/modules/esnext.map.find.js"; import "core-js/modules/esnext.map.find-key.js"; import "core-js/modules/esnext.map.includes.js"; import "core-js/modules/esnext.map.key-of.js"; import "core-js/modules/esnext.map.map-keys.js"; import "core-js/modules/esnext.map.map-values.js"; import "core-js/modules/esnext.map.merge.js"; import "core-js/modules/esnext.map.reduce.js"; import "core-js/modules/esnext.map.some.js"; import "core-js/modules/esnext.map.update.js"; import "core-js/modules/esnext.symbol.metadata.js"; let _initProto, _startRemoteControlRequestDecs, _acceptRemoteControlRequestDecs, _rejectRemoteControlRequestDecs, _terminateRemoteControlDecs, _updateRemoteControlConnectionStateDecs; import "core-js/modules/es.json.stringify.js"; 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; } import { FcrRemoteControlState } from './type'; import { AgoraObservable, md5, Duration, ActionWhenTaskFail, AgoraScheduler, trace, DetailErrorCode, ErrorModuleCode } from '../imports'; import validateParams from '../utilities/validate-params'; import { createLogger, generateLogObserver } from '../utilities/logger'; import { fcrRemoteControlConnectionStateTypeSchema, fcrRemoteControlSessionParamsSchema, stringKeyUnknownValueSchema, stringSchema } from '../schema'; import { generateFcrCoreClientError, generateFcrCoreServerError } from '../utilities/error'; import { bound } from '../imports'; export const KEEPALIVE_TIMEOUT_MULTIPLIER = 2; export class FcrRemoteControlImpl { static { [_initProto] = _applyDecs(this, [[_startRemoteControlRequestDecs, 2, "startRemoteControlRequest"], [_acceptRemoteControlRequestDecs, 2, "acceptRemoteControlRequest"], [_rejectRemoteControlRequestDecs, 2, "rejectRemoteControlRequest"], [_terminateRemoteControlDecs, 2, "terminateRemoteControl"], [_updateRemoteControlConnectionStateDecs, 2, "updateRemoteControlConnectionState"], [trace, 2, "getRemoteControlState"], [bound, 2, "_notifyRemoteControlState"]], []).e; } [(_startRemoteControlRequestDecs = [trace(['params']), validateParams(fcrRemoteControlSessionParamsSchema)], _acceptRemoteControlRequestDecs = [trace(['sessionId', 'cause']), validateParams(stringSchema, stringKeyUnknownValueSchema.optional())], _rejectRemoteControlRequestDecs = [trace(['sessionId', 'cause']), validateParams(stringSchema, stringKeyUnknownValueSchema.optional())], _terminateRemoteControlDecs = [trace(['sessionId', 'cause']), validateParams(stringSchema, stringKeyUnknownValueSchema.optional())], _updateRemoteControlConnectionStateDecs = [trace(['sessionId', 'state', 'cause']), validateParams(stringSchema, fcrRemoteControlConnectionStateTypeSchema, stringKeyUnknownValueSchema.optional())], "logger")] = (_initProto(this), createLogger({ prefix: 'FcrRemoteControlImpl' })); _remoteControlSessionKey = 'remote-control'; _observable = new AgoraObservable(); //@internal _taskInterval = Duration.second(1); //@internal //@internal _engineObserver = { onPeerMessageReceived: this._handleRemoteControlMessage.bind(this) }; //@internal 控制端会话信息 _activeRemoteControlSession = null; //@internal 被控制端会话信息 _receivedRemoteControlSession = null; constructor(_rteEngine, _api, _userId) { this._rteEngine = _rteEngine; this._api = _api; this._userId = _userId; this._addLogObserver(); this._executeTask = this._executeTask.bind(this); this._rteEngine.addObserver(this._engineObserver); this._task = AgoraScheduler.shared.addPollingTask(this._executeTask, this._taskInterval, true, () => ActionWhenTaskFail.CONTINUE); this.logger.info(`remote control initialized FcrRemoteControlImpl userId: ${this._userId}`); } //@internal async _handleRemoteControlMessage(message) { const { senderId, payload } = message; const { data, cmd } = payload; const logContext = `cmd=${cmd}, sessionKey=${data.sessionKey}, sessionId=${data.sessionUuid}, ` + `action=${String(data.action)}, senderId=${senderId}, keepAlive=${!!data.keepAlive}`; if (data.sessionKey !== this._remoteControlSessionKey) { this.logger.info(`[RemoteControl] ignore peer message because sessionKey does not match remote-control: ${logContext}`); return; } if (cmd === '1001') { if (!data.keepAlive) { this.logger.info(`[RemoteControl] ignore remote-control update without keepAlive: ${logContext}`); return; } this.logger.info(`[RemoteControl] forward update session for handling: ${logContext}`); return await this._handleUpdateRemoteControlSession({ senderId: senderId, sessionKey: data.sessionKey, sessionId: data.sessionUuid, payload: data.payload, duration: data.duration, interval: data.interval, action: data.action, keepAlive: data.keepAlive, timestamp: message.timestamp }); } else if (cmd === '1002') { this.logger.info(`[RemoteControl] forward delete session for handling: ${logContext}`); return await this._handleDeleteRemoteControlSession({ senderId: senderId, sessionKey: data.sessionKey, sessionId: data.sessionUuid, payload: data.payload, duration: data.duration, interval: data.interval, action: data.action, timestamp: message.timestamp }); } else { this.logger.info(`[RemoteControl] ignore unsupported cmd: ${logContext}`); } } async _handleUpdateRemoteControlSession(data) { const { action, keepAlive, ...session } = data; if (this._isSessionExpired({ timestamp: session.timestamp, duration: session.duration })) { this.terminateRemoteControl(session.sessionId); return; } const { state, cause } = data.payload; const _cause = cause ?? {}; switch (action) { case 0: { if (!this._receivedRemoteControlSession) { this._receivedRemoteControlSession = { sessionId: session.sessionId, session: { cause: _cause, controllerId: session.senderId, duration: session.duration, controlleeId: this._userId, timestamp: session.timestamp, interval: session.interval, nextTs: Date.now() + session.interval, startTs: Date.now(), action }, state }; this.logger.info(`on received remote control session request ${JSON.stringify(session)}`); this._notifyRemoteControlState(state, { sessionId: session.sessionId, fromUserId: session.senderId, receiverId: this._userId, controllerId: session.senderId, controlleeId: this._userId, cause: _cause }); } // 如果已经存在一个接收的远程控制请求, 则拒绝新的请求 else if (this._receivedRemoteControlSession && this._receivedRemoteControlSession.sessionId !== session.sessionId) { return await this._rejectSession({ sessionId: session.sessionId, receiverId: session.senderId, payload: { cause: { code: 'remote_control_has_been_controlled' } }, sessionKey: this._remoteControlSessionKey }); } // 相同的状态则是维系心跳 this._receivedRemoteControlSession.session = { ...this._receivedRemoteControlSession.session, nextTs: Date.now() + session.interval, action }; break; } case 1: { if (!this._activeRemoteControlSession && !this._receivedRemoteControlSession) { this.logger.error(`on upadte remote control session state: ${state}, session: ${JSON.stringify(session)}, but local remote control session not found`); return; } this.logger.info(`on upadte remote control session state: ${state}, session: ${JSON.stringify(session)}`); if (this._receivedRemoteControlSession && this._receivedRemoteControlSession.sessionId === session.sessionId) { let oldState = this._receivedRemoteControlSession.state; this._receivedRemoteControlSession.state = state; this._receivedRemoteControlSession.session = { ...this._receivedRemoteControlSession.session, cause: _cause, action, nextTs: Date.now() + session.interval, startTs: Date.now() }; // 如果来自心跳,相同状态下就不要上报了 if (oldState === state) { return; } this._notifyRemoteControlState(state, { sessionId: this._receivedRemoteControlSession.sessionId, controllerId: this._receivedRemoteControlSession.session.controllerId, controlleeId: this._receivedRemoteControlSession.session.controlleeId, fromUserId: session.senderId, receiverId: this._userId, cause: _cause }); } else if (this._activeRemoteControlSession && this._activeRemoteControlSession.sessionId === session.sessionId) { let oldState = this._activeRemoteControlSession.state; this._activeRemoteControlSession.state = state; this._activeRemoteControlSession.session = { ...this._activeRemoteControlSession.session, cause: _cause, action }; // 如果来自心跳,相同状态下就不要上报了 if (oldState === state) { return; } this._notifyRemoteControlState(state, { sessionId: this._activeRemoteControlSession.sessionId, controllerId: this._activeRemoteControlSession.session.controllerId, controlleeId: this._activeRemoteControlSession.session.controlleeId, fromUserId: session.senderId, receiverId: this._userId, cause: _cause }); } break; } } } async _handleDeleteRemoteControlSession(data) { const { action, ...session } = data; const { state, cause } = data.payload; const _cause = cause ?? {}; switch (action) { case 2: { if (this._activeRemoteControlSession && this._activeRemoteControlSession.sessionId === session.sessionId) { this._notifyRemoteControlState(FcrRemoteControlState.REQUEST_REJECTED, { sessionId: session.sessionId, controllerId: this._activeRemoteControlSession.session.controllerId, controlleeId: this._activeRemoteControlSession.session.controlleeId, fromUserId: session.senderId, receiverId: this._userId, cause: _cause }); this._activeRemoteControlSession = null; this.logger.info(`on reject remote control session state: ${state}, ${JSON.stringify(session)}`); } break; } case 3: { const isActiveSession = this._activeRemoteControlSession && this._activeRemoteControlSession.sessionId === session.sessionId; const isReceivedSession = this._receivedRemoteControlSession && this._receivedRemoteControlSession.sessionId === session.sessionId; const _session = isActiveSession ? this._activeRemoteControlSession : isReceivedSession ? this._receivedRemoteControlSession : null; if (_session) { this._notifyRemoteControlState(FcrRemoteControlState.SESSION_ENDED, { sessionId: session.sessionId, fromUserId: session.senderId, receiverId: this._userId, cause: _cause, controllerId: _session.session.controllerId, controlleeId: _session.session.controlleeId }); this._activeRemoteControlSession = null; this._receivedRemoteControlSession = null; this.logger.info(`on delete remote control session state: ${state}, ${JSON.stringify(session)}`); } break; } default: break; } } //@internal async _terminateRemoteControlBestEffort(sessionId, context) { try { await this.terminateRemoteControl(sessionId); } catch (e) { this.logger.error(`remote control ${context}: ${e}`); } } //@internal async _executeTask() { // ping sessions (sender side) if (this._activeRemoteControlSession) { const { sessionId, session, state } = this._activeRemoteControlSession; if (this._isSessionExpired({ duration: session.duration, timestamp: session.timestamp })) { this.logger.info(`remote control session expired, removed: ${sessionId}`); await this._terminateRemoteControlBestEffort(sessionId, 'terminate remote control session on active side expiry'); return; } if (session.nextTs < Date.now()) { this.logger.info(`ping remote control session: ${sessionId}`); try { const { data: { interval } } = await this._api.updatePeerSession({ userId: this._userId, sessionId, sessionKey: this._remoteControlSessionKey, duration: session.duration, payload: { state, cause: session.cause }, targetId: session.controlleeId, keepAlive: true, action: session.action }); const nextTs = Date.now() + interval; this._activeRemoteControlSession.session = { ...session, nextTs }; this.logger.info(`refresh remote control session schedule: ${sessionId}, next ts: ${nextTs}`); } catch (e) { this.logger.error(`ping remote control session failed: ${e}`); } } } if (this._receivedRemoteControlSession) { const { sessionId, session } = this._receivedRemoteControlSession; if (this._isSessionExpired({ duration: session.duration, timestamp: session.timestamp })) { this.logger.info(`remote control session expired, removed: ${sessionId}`); await this._terminateRemoteControlBestEffort(sessionId, 'terminate remote control session on received side expiry'); return; } if (session.nextTs + session.interval * KEEPALIVE_TIMEOUT_MULTIPLIER < Date.now()) { await this._terminateRemoteControlBestEffort(sessionId, 'terminate remote control session on keepalive timeout'); if (!this._receivedRemoteControlSession) { this.logger.info(`remote control session has not been pinged, removed: ${sessionId}`); } } } } //@internal _isSessionExpired(session) { return session.timestamp + session.duration * Duration.second(1) < Date.now(); } //@internal async _rejectSession(session) { try { await this._api.deletePeerSession({ userId: this._userId, sessionId: session.sessionId, sessionKey: session.sessionKey, receiverIds: [session.receiverId], action: 2, payload: { cause: session.payload.cause ?? {} } }); } catch (e) { throw generateFcrCoreServerError(DetailErrorCode.LOCAL_HTTP_REQUEST_FAILED, 'reject remote control session failed'); } } async startRemoteControlRequest(params) { // 如果会话已存在,则抛出错误(这种情况不应该发生,因为会话id是唯一的, 也可能是keepAlive的会话, 但是如果再次调用startPeerSession, 则说明有bug) if (this._activeRemoteControlSession || this._receivedRemoteControlSession) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.PEER_SESSION_ALREADY_EXISTS, 'remote control session already exists'); } try { const _now = Date.now(); const sessionId = md5(`${_now}`); const { data: { interval } } = await this._api.updatePeerSession({ userId: this._userId, sessionId, sessionKey: this._remoteControlSessionKey, duration: params.duration, payload: { cause: params.cause ?? {}, state: FcrRemoteControlState.REQUEST_RECEIVED }, targetId: params.controlleeId, keepAlive: true, action: 0 }); const nextTs = _now + interval; this._activeRemoteControlSession = { sessionId, session: { ...params, interval, nextTs, startTs: _now, action: 0 }, state: FcrRemoteControlState.REQUEST_RECEIVED }; this.logger.info(`remote control put into session activeRemote: ${sessionId}, next ts: ${nextTs}, remain: ${interval}ms`); return sessionId; } catch (e) { throw generateFcrCoreServerError(DetailErrorCode.LOCAL_HTTP_REQUEST_FAILED, 'start remote control session failed'); } } async acceptRemoteControlRequest(_sessionId, cause) { if (!this._receivedRemoteControlSession) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.UNDEFINED, 'remote control session not found'); } const { session, sessionId, state } = this._receivedRemoteControlSession; if (_sessionId !== sessionId) { return; } if (session && state === FcrRemoteControlState.REQUEST_RECEIVED) { const { data: { interval } } = await this._api.updatePeerSession({ userId: this._userId, sessionId, sessionKey: this._remoteControlSessionKey, duration: session.duration, payload: { cause: cause ?? {}, state: FcrRemoteControlState.REQUEST_ACCEPTED }, targetId: session.controllerId, keepAlive: true, action: 1 }); const _now = Date.now(); this._receivedRemoteControlSession.session = { ...session, nextTs: _now + interval, startTs: _now, action: 1 }; this._receivedRemoteControlSession.state = FcrRemoteControlState.REQUEST_ACCEPTED; this._notifyRemoteControlState(FcrRemoteControlState.REQUEST_ACCEPTED, { sessionId, fromUserId: this._userId, receiverId: session.controllerId, controllerId: session.controllerId, controlleeId: session.controlleeId, cause: cause ?? {} }); return; } } async rejectRemoteControlRequest(_sessionId, cause) { if (!this._receivedRemoteControlSession) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.UNDEFINED, 'remote control session not found'); } const { session, sessionId } = this._receivedRemoteControlSession; if (_sessionId !== sessionId) { return; } return await this._rejectSession({ sessionId, receiverId: session.controllerId, payload: { cause: cause ?? {} }, sessionKey: this._remoteControlSessionKey }).then(() => { this._notifyRemoteControlState(FcrRemoteControlState.REQUEST_REJECTED, { sessionId, fromUserId: session.controlleeId, receiverId: session.controllerId, controllerId: session.controllerId, controlleeId: session.controlleeId, cause }); if (sessionId === this._receivedRemoteControlSession?.sessionId) { this._receivedRemoteControlSession = null; } }); } async terminateRemoteControl(_sessionId, cause) { const isActiveSession = this._activeRemoteControlSession && this._activeRemoteControlSession.sessionId === _sessionId; const isReceivedSession = this._receivedRemoteControlSession && this._receivedRemoteControlSession.sessionId === _sessionId; const session = isActiveSession ? this._activeRemoteControlSession : isReceivedSession ? this._receivedRemoteControlSession : null; if (session) { const _cause = cause ?? {}; try { return await this._api.deletePeerSession({ userId: this._userId, sessionId: _sessionId, sessionKey: this._remoteControlSessionKey, receiverIds: this._userId === session.session.controlleeId ? [session.session.controllerId] : [session.session.controlleeId], action: 3, payload: { cause: _cause, state: FcrRemoteControlState.SESSION_ENDED } }).then(() => { if (this._activeRemoteControlSession || this._receivedRemoteControlSession) { this._notifyRemoteControlState(FcrRemoteControlState.SESSION_ENDED, { sessionId: _sessionId, fromUserId: this._userId, receiverId: this._userId === session.session.controlleeId ? session.session.controllerId : session.session.controlleeId, cause: _cause, controllerId: session.session.controllerId, controlleeId: session.session.controlleeId }); this.logger.info(`delete remote control session state: ${FcrRemoteControlState.SESSION_ENDED}, ${JSON.stringify(session)}`); } this._activeRemoteControlSession = null; this._receivedRemoteControlSession = null; }); } catch (e) { throw generateFcrCoreServerError(DetailErrorCode.LOCAL_HTTP_REQUEST_FAILED, 'remote control end session failed'); } } throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.UNDEFINED, 'remote control session not found'); } async updateRemoteControlConnectionState(_sessionId, _state, cause) { const isActiveSession = this._activeRemoteControlSession && this._activeRemoteControlSession.sessionId === _sessionId; const isReceivedSession = this._receivedRemoteControlSession && this._receivedRemoteControlSession.sessionId === _sessionId; const sessionInfo = isActiveSession ? this._activeRemoteControlSession : isReceivedSession ? this._receivedRemoteControlSession : null; if (sessionInfo) { if (sessionInfo.state === _state) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.REMOTE_CONTROL_SESSION_STATE_NOT_MATCH, `on update remote control session state: ${_state} already existed`); } switch (_state) { case FcrRemoteControlState.CONNECT_INITIALIZED: await this._initializeRemoteControlConnection(sessionInfo, cause); break; case FcrRemoteControlState.CONNECT_ESTABLISHED: await this._establishRemoteControlConnection(sessionInfo, cause); break; } return; } throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.UNDEFINED, 'remote control session not found for update connection state'); } //@internal async _initializeRemoteControlConnection(_session, cause) { const { session, state, sessionId } = _session; if (state !== FcrRemoteControlState.REQUEST_ACCEPTED) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.REMOTE_CONTROL_SESSION_STATE_NOT_MATCH, 'remote control session state not match for initialize connection, current state: ' + state + ', expected state: ' + FcrRemoteControlState.REQUEST_ACCEPTED); } const targetId = this._userId === session.controlleeId ? session.controllerId : session.controlleeId; const { data: { interval } } = await this._api.updatePeerSession({ userId: this._userId, sessionId, sessionKey: this._remoteControlSessionKey, duration: session.duration, payload: { cause: cause ?? {}, state: FcrRemoteControlState.CONNECT_INITIALIZED }, targetId, keepAlive: true, action: 1 }); const _now = Date.now(); _session.session = { ...session, nextTs: _now + interval, startTs: _now, action: 1 }; _session.state = FcrRemoteControlState.CONNECT_INITIALIZED; this._notifyRemoteControlState(FcrRemoteControlState.CONNECT_INITIALIZED, { sessionId, fromUserId: this._userId, receiverId: targetId, controllerId: session.controllerId, controlleeId: session.controlleeId, cause: cause ?? {} }); return; } //@internal async _establishRemoteControlConnection(_session, cause) { const { session, state, sessionId } = _session; if (state !== FcrRemoteControlState.CONNECT_INITIALIZED) { throw generateFcrCoreClientError(ErrorModuleCode.FCR_REMOTE_CONTROL, DetailErrorCode.REMOTE_CONTROL_SESSION_STATE_NOT_MATCH, 'remote control session state not match for establish connection, current state: ' + state + ', expected state: ' + FcrRemoteControlState.CONNECT_INITIALIZED); } const targetId = this._userId === session.controlleeId ? session.controllerId : session.controlleeId; const { data: { interval } } = await this._api.updatePeerSession({ userId: this._userId, sessionId, sessionKey: this._remoteControlSessionKey, duration: session.duration, payload: { cause: cause ?? {}, state: FcrRemoteControlState.CONNECT_ESTABLISHED }, targetId, keepAlive: true, action: 1 }); const _now = Date.now(); _session.session = { ...session, nextTs: _now + interval, startTs: _now, action: 1 }; _session.state = FcrRemoteControlState.CONNECT_ESTABLISHED; this._notifyRemoteControlState(FcrRemoteControlState.CONNECT_ESTABLISHED, { sessionId, fromUserId: this._userId, receiverId: targetId, controllerId: session.controllerId, controlleeId: session.controlleeId, cause: cause ?? {} }); return; } getRemoteControlState() { // 不可能存在两个session, 如果是控制端则存在一个activeSession, 如果是被控制端则存在一个receivedSession, 所以这里直接返回state即可 if (this._activeRemoteControlSession) { return this._activeRemoteControlSession.state; } if (this._receivedRemoteControlSession) { return this._receivedRemoteControlSession.state; } return null; } addObserver(observer) { this._observable.addObserver(observer); } removeObserver(observer) { this._observable.removeObserver(observer); } async release() { if (this._task) { this._task.stop(); this._task = undefined; } if (this._activeRemoteControlSession) { await this._terminateRemoteControlBestEffort(this._activeRemoteControlSession.sessionId, 'release: terminate active session'); } if (this._receivedRemoteControlSession) { await this._terminateRemoteControlBestEffort(this._receivedRemoteControlSession.sessionId, 'release: terminate received session'); } this._rteEngine.removeObserver(this._engineObserver); this._observable.removeAllObservers(); this._activeRemoteControlSession = null; this._receivedRemoteControlSession = null; } _notifyRemoteControlState(state, extra) { this._observable.notifyObservers('onRemoteControlStateChanged', state, extra); } _addLogObserver() { this.addObserver(generateLogObserver(this.logger, [['onRemoteControlStateChanged', ['state', 'extra']]])); } }