sinch-rtc
Version:
RTC JavaScript/Web SDK
98 lines • 3.94 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubSubClientFactory = exports.PubSubClient = void 0;
const PubNub = require("pubnub");
const Event_1 = require("../utils/Event");
class PubSubClient {
constructor(broadcastChannel, signalChannel, config) {
this.broadcastChannel = broadcastChannel;
this.signalChannel = signalChannel;
this.config = config;
this.subscribers = new Array();
this.onBroadcastMessage = new Event_1.Event();
this.onSignalMessage = new Event_1.Event();
this.broadcastFilters = new Array();
this.broadcast = new PubNub({
subscribeKey: config.broadcastSubscribeKey,
// Setting ssl to true because connecting to PubSub service requires https
ssl: true,
origin: config.hostname,
uuid: PubNub.generateUUID(),
});
this.signal = new PubNub({
subscribeKey: config.signalSubscribeKey,
publishKey: config.signalPublishKey,
ssl: true,
origin: config.hostname,
uuid: PubNub.generateUUID(),
});
this.signal.addListener({
message: (signalMessage) => {
const { message } = signalMessage;
this.onSignalMessage.fire({ message });
},
});
this.broadcast.addListener({
message: (broadcastMessage) => {
const { message } = broadcastMessage;
this.broadcastFilters.forEach((f) => {
if (f(message)) {
this.onBroadcastMessage.fire({ message });
}
});
},
});
}
subscribe() {
this.subscribeOn(this.signal, this.signalChannel, 0);
}
subscribeOn(subscriber, channel, rewindMs = 0) {
if (!this.subscribers.includes(channel)) {
this.subscribers.push(channel);
if (rewindMs > 0) {
subscriber.time((status, response) => {
const startTt = response.timetoken - rewindMs * 10000;
subscriber.subscribe({
channels: [channel],
timetoken: startTt,
});
});
}
else {
subscriber.subscribe({ channels: [channel] });
}
}
}
publish(channel, message) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.signal.publish({ channel, message });
});
}
broadcastSubscribe(predicate) {
this.broadcastFilters.push(predicate);
this.subscribeOn(this.broadcast, this.broadcastChannel, 60000);
}
cancel() {
this.subscribers.splice(0);
this.signal.unsubscribeAll();
this.broadcast.unsubscribeAll();
this.broadcastFilters.splice(0);
}
}
exports.PubSubClient = PubSubClient;
class PubSubClientFactory {
static create(broadcastChannel, signalChannel, config) {
return new PubSubClient(broadcastChannel, signalChannel, config);
}
}
exports.PubSubClientFactory = PubSubClientFactory;
//# sourceMappingURL=PubSubClient.js.map