janky
Version:
Janus Interface
114 lines (102 loc) • 3.89 kB
text/typescript
import { JanusJS } from "janus-gateway";
import LocalRTC from "../repositories/LocalRTC";
import { HandleRemoteSDPCallback } from "../types";
import eventEmitter from "../repositories/eventEmitter";
import { PUBLISHING_EVENTS } from "../events";
interface PublisherEventsHandlerOptions {
msg: JanusJS.Message;
jsep: JanusJS.JSEP;
handleRemoteSDP: HandleRemoteSDPCallback;
sendOffer: () => void;
handlePublishers: (list: any) => void;
}
class PublisherEventsHandler {
private event: string = '';
private readonly msg: JanusJS.Message;
private readonly jsep: JanusJS.JSEP;
private readonly handleRemoteSDP: HandleRemoteSDPCallback
private readonly sendOffer: () => void;
private readonly handlePublishers: (list: any) => void;
constructor({
msg,
jsep,
handleRemoteSDP,
sendOffer,
handlePublishers
}: PublisherEventsHandlerOptions){
this.msg = msg;
this.jsep = jsep;
this.handleRemoteSDP = handleRemoteSDP;
this.sendOffer = sendOffer;
this.handlePublishers = handlePublishers;
this.event = msg.videoroom;
this.handleMessageEventHanlder();
this.messageErrorHandler();
this.handleMessageJsep();
}
private async handleMessageEventHanlder() {
console.log(this.msg, this.jsep);
if(!this.event || this.event === '') return;
switch(this.event) {
case 'joined':
//handle a user joined
console.log('User has joined', this.msg);
const { publishers, id, private_id: privateID } = this.msg; // To do something with publishers
this.handlePublishers(publishers);
//Save IDS
LocalRTC.setIDs(id, privateID);
this.sendOffer();
break;
case 'destroyed':
console.log('Room is destroyed', this.msg);
//handle a room destroyed
break;
case 'talking':
console.log('someone is talking', this.msg);
//handle a user talking
break;
case 'stopped-talking':
console.log('Someone stopped talking', this.msg);
//handle a user stopped talking
break;
case 'event':
if(this.msg.joining) {
//handle new one is joining
console.log('Someone is joining yaaay', this.msg);
}
if(this.msg.streams) {
console.log('Something to do with streams', this.msg.streams);
}
if(this.msg.publishers) {
// handle new feeds
this.handlePublishers(this.msg.publishers);
}
if(this.msg.leaving) {
// handle a user is leaving
console.log('Owww, is leaving :(', this.msg.leaving);
}
if(this.msg.unpublished) {
console.log('someone has just shutup', this.msg);
eventEmitter.emit(PUBLISHING_EVENTS.UNPUBLISH, this.msg.unpublished);
// handle a user stopped publishing
}
break;
}
}
private messageErrorHandler() {
if(!this.msg.error) return;
if(this.msg.error_code === 426) {
// Handle no room for this session
console.log('Oh no, joining to an alien room', this.msg);
return;
}
// Normal error handling
console.log('Dammit!', this.msg.error);
}
private handleMessageJsep () {
if(!this.jsep) return;
this.handleRemoteSDP(this.msg, this.jsep);
console.log('you piece and me piece');
}
};
export default PublisherEventsHandler;