sinch-rtc
Version:
RTC JavaScript/Web SDK
105 lines • 4.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MxpMessageChannel = void 0;
const Event_1 = require("../utils/Event");
const Errors_1 = require("../utils/Errors");
const utils_1 = require("../utils");
class MxpMessageChannel {
constructor(sessionId, instanceId, protocol) {
this.sessionId = sessionId;
this.instanceId = instanceId;
this.protocol = protocol;
this.onInboundMessage = new Event_1.Event();
this.onOutboundMessage = new Event_1.Event();
this.pendingInbound = new Array();
this.pendingOutbound = new Array();
this.sendOutboundMessage = (message) => {
if (!this.sessionKey) {
this.pendingOutbound.push(message);
return;
}
if (!this.channelId)
throw new Errors_1.InvalidOperationError("No channelId configured");
if (!this.onOutboundMessage)
throw new Errors_1.InvalidOperationError("No outbound message event handler configured");
this.logOutboundMessage(message);
this.protocol.encode(message, this.sessionKey).forEach((data) => {
if (this.channelId)
this.onOutboundMessage.fire({
channelId: this.channelId,
data,
message,
});
});
};
}
configure(sessionKey, channelId) {
if (!channelId)
throw new Errors_1.ArgumentError("Invalid channelId", (0, utils_1.nameof)(channelId));
this.sessionKey = sessionKey;
this.channelId = channelId;
this.processPendingOutbound();
}
isMessageForThisSession(sessionId) {
return sessionId == this.sessionId;
}
handleInboundTransportMessage(signalMessage, mxpEventsCollector) {
const { message } = signalMessage;
if (!message)
throw new Errors_1.ArgumentError("Invalid input (empty)", (0, utils_1.nameof)(message));
if (!this.sessionKey) {
this.pendingInbound.push(signalMessage);
return;
}
this.processInboundTransportMessage(signalMessage, mxpEventsCollector);
}
processInboundTransportMessage(signalMessage, mxpEventsCollector) {
if (!this.sessionKey) {
console.log("Session key is not available, cannot process message");
return;
}
const { message } = signalMessage;
const decoded = this.protocol.decode(message, this.sessionKey);
if (decoded) {
if (!this.isMessageForThisSession(decoded.transport.sessionId)) {
const error = `Received MXP message for another session: ${decoded.transport.sessionId}, silently ignore`;
console.log(error);
mxpEventsCollector === null || mxpEventsCollector === void 0 ? void 0 : mxpEventsCollector.addErrorEventLog({
sessionId: decoded.transport.sessionId,
localTimeStamp: Date.now().toString(),
additionalInfo: JSON.stringify({ error }),
});
return;
}
if (decoded.message.isAddressedToInstance(this.instanceId)) {
mxpEventsCollector === null || mxpEventsCollector === void 0 ? void 0 : mxpEventsCollector.addReceivedEventLogForMessage(decoded.message);
this.handleInboundMessage(decoded.message);
}
else {
console.log("Message not for this instance:", decoded.message);
}
}
}
handleInboundMessage(message) {
this.logInboundMessage(message);
this.onInboundMessage.fire(message);
}
processPendingInbound() {
this.pendingInbound.splice(0).forEach((m) => {
this.processInboundTransportMessage(m);
});
}
processPendingOutbound() {
this.pendingOutbound.splice(0).forEach((m) => {
this.sendOutboundMessage(m);
});
}
logInboundMessage(m) {
console.log("Inbound MXP message", m);
}
logOutboundMessage(m) {
console.log("Outbound MXP message", m);
}
}
exports.MxpMessageChannel = MxpMessageChannel;
//# sourceMappingURL=MxpMessageChannel.js.map