@ketch-in/packet
Version:
149 lines • 5.28 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Observable_1 = __importDefault(require("./Observable"));
//@ts-ignore
const rtcmulticonnection_1 = __importDefault(require("rtcmulticonnection"));
const socket_io_client_1 = require("socket.io-client");
//@ts-ignore
const thisGlobal = typeof window ? window : global;
thisGlobal.io = socket_io_client_1.io;
const CHUNK_SIZE = 60 * 1000;
const SOCKET_MESSAGE_EVENT = "data-sharing";
class WebRTC extends Observable_1.default {
constructor(meetId, signalingUrl) {
super();
this.meetId = meetId;
this.signalingUrl =
signalingUrl[signalingUrl.length - 1] === "/"
? signalingUrl
: `${signalingUrl}/`;
}
initialize() {
if (this.connection) {
this.close();
}
if (!this.meetId || !this.signalingUrl) {
this.emit("onError", null);
return;
}
this.connection = new rtcmulticonnection_1.default();
this.connection.socketURL = this.signalingUrl;
this.connection.socketMessageEvent = SOCKET_MESSAGE_EVENT;
this.connection.chunkSize = CHUNK_SIZE;
this.connection.sdpConstraints.mandatory = {
OfferToReceiveAudio: false,
OfferToReceiveVideo: false,
};
this.connection.session = { data: true };
this.connection.checkPresence(this.meetId, (isRoomExist) => this.onConnection(isRoomExist));
this.connection.onopen = (connectionInfo) => {
// new connection
this.emit("onNewConnect", connectionInfo);
};
this.connection.onmessage = (e) => {
this.emit("onMessage", e.data.type, e.data.payload);
};
this.connection.onerror = (error) => {
this.emit("onError", error);
};
this.connection.onSocketDisconnect = (event) => {
this.destroy();
};
this.connection.socket.on("onDisconnect", (...args) => {
this.emit("onDisconnect", ...args);
});
this.connection.socket.on("userid-already-taken", (...args) => {
this.emit("onChangedUserId", this.connection.userid);
});
this.connection.onleave = (connectionInfo) => {
this.emit("onLeave", connectionInfo);
};
}
close() {
if (this.connection === null) {
return;
}
// disconnect with all users
this.connection.getAllParticipants().forEach(function (pid) {
try {
this.connection.disconnectWith(pid);
}
catch (_a) { }
});
// stop all local cameras
this.connection.attachStreams.forEach(function (localStream) {
localStream.stop();
});
try {
// close socket.io connection
this.connection.closeSocket();
}
catch (_a) { }
this.connection = null;
}
// 연결을 관리합니다.
onConnection(isRoomExist) {
// _ : isRoomJoined -> 방이 열린 여부를 나타냅니다.
// __ : roomId -> 룸 ID를 나타냅니다.
const done = (_, __, error) => {
if (!error) {
this.emit("onOpen", this.connection.userid);
return;
}
// 이미 방이 생성된 상태라면 가입을 시도합니다.
if (!isRoomExist && error === this.connection.errors.ROOM_NOT_AVAILABLE) {
this.onConnection(isRoomExist);
return;
}
// 그 외의 에러인 경우 에러를 반환합니다.
this.connection.onerror(error);
};
// sessionId가 존재하지 않으면 meetId로 초기화합니다.
if (!this.connection.sessionid) {
this.connection.sessionid = this.meetId;
}
if (!isRoomExist) {
// 방이 없는 경우 생성자로써 방을 열기를 시도합니다.
this.connection.isInitiator = !isRoomExist;
this.connection.open(this.meetId, done);
return;
}
this.connection.join(this.meetId, done);
}
// connection 부분을 초기화합니다.
reconnection() {
this.close();
this.initialize();
}
// WebRTC로 message를 전송합니다.
sendMessage(type, payload) {
if (this.getDestroy()) {
return;
}
this.connection.send({ type, payload });
}
// 옵션을 변경시, reconnection이 발생합니다.
setOptions({ meetId, signalingUrl }) {
let changed = false;
if (meetId && this.meetId !== meetId) {
this.meetId = meetId;
changed = true;
}
if (signalingUrl && this.signalingUrl !== signalingUrl) {
this.signalingUrl = signalingUrl;
changed = true;
}
if (changed) {
this.reconnection();
}
}
destroy() {
super.destroy();
this.close();
}
}
exports.default = WebRTC;
//# sourceMappingURL=WebRTC.js.map