wrtc-signalling-client
Version:
A WebRTC signalling client for lbarcl/webrtc-signalling-server
128 lines • 4.71 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());
});
};
import Pusher from "pusher-js";
class EventEmitter {
constructor() {
this.listeners = {};
}
on(eventName, listener) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = [];
}
this.listeners[eventName].push(listener);
}
off(eventName, listener) {
if (this.listeners[eventName]) {
this.listeners[eventName] = this.listeners[eventName].filter((l) => l !== listener);
}
}
emit(eventName, data) {
const event = {
name: eventName,
data: data,
};
if (this.listeners[eventName]) {
this.listeners[eventName].forEach((listener) => listener(event));
}
}
}
class Signal extends EventEmitter {
constructor(pusherKey, signallingServer, pusherOptions) {
super();
this.channel = null;
this.roomId = null;
this.signallingServer = signallingServer;
this.pusher = new Pusher(pusherKey, pusherOptions);
this.pusher.connect();
this.pusher.bind("connected", () => {
this.socketId = this.pusher.connection.socket_id;
});
}
createRoom() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield fetch(`${this.signallingServer}/rooms/create`, { method: "POST" });
const id = yield response.text();
this.joinRoom(id);
return id;
});
}
sendOffer(offer) {
return __awaiter(this, void 0, void 0, function* () {
if (this.roomId) {
yield fetch(`${this.signallingServer}/rooms/${this.roomId}/offer`, { method: "POST", body: offer });
const response = yield fetch(`${this.signallingServer}/rooms/${this.roomId}/answer`);
return response.text();
}
else {
throw new Error("No room id available");
}
});
}
getOffer(id) {
return __awaiter(this, void 0, void 0, function* () {
if (id) {
this.joinRoom(id);
const response = yield fetch(`${this.signallingServer}/rooms/${id}/offer`);
return response.text();
}
else {
throw new Error("No room id available");
}
});
}
sendAnswer(answer) {
if (this.roomId) {
fetch(`${this.signallingServer}/rooms/${this.roomId}/answer`, { method: "POST", body: answer });
}
else {
throw new Error("No room id available");
}
}
sendIce(ice) {
return __awaiter(this, void 0, void 0, function* () {
const send = () => __awaiter(this, void 0, void 0, function* () {
if (!this.socketId) {
this.socketId = this.pusher.connection.socket_id;
setTimeout(send, 500);
}
else {
yield fetch(`${this.signallingServer}/rooms/${this.roomId}/ice?socketID=${this.socketId}`, { method: "POST", body: ice });
}
});
if (this.roomId) {
yield send();
}
else {
throw new Error("No room id available");
}
});
}
closeRoom() {
return __awaiter(this, void 0, void 0, function* () {
if (this.roomId && this.channel) {
yield fetch(`${this.signallingServer}/rooms/${this.roomId}`, { method: "DELETE" });
this.channel.unsubscribe();
this.channel = null;
}
else {
throw new Error("No channel or room id available");
}
});
}
joinRoom(id) {
this.roomId = id;
this.channel = this.pusher.subscribe(`cache-${id}`);
this.channel.bind("ice", (ice) => {
this.emit("ice", ice);
});
}
}
export default Signal;
//# sourceMappingURL=index.js.map