UNPKG

janky

Version:

Janus Interface

93 lines (81 loc) 2.89 kB
import { JanusJS } from "janus-gateway"; import LocalRTC from "../repositories/LocalRTC"; interface SubscriberEventsHandlerOptions { msg: JanusJS.Message; jsep: JanusJS.JSEP; handleRemoteSDP: () => void; } class SubscriberEventsHandler { private event: string = ''; private readonly msg: JanusJS.Message; private readonly jsep: JanusJS.JSEP; private readonly handleRemoteSDP: () => void; constructor({ msg, jsep, handleRemoteSDP, }: SubscriberEventsHandlerOptions){ this.msg = msg; this.jsep = jsep; this.handleRemoteSDP = handleRemoteSDP; this.event = msg.videoroom; this.handleMessageEventHanlder(); this.messageErrorHandler(); this.handleMessageJsep(); } private async handleMessageEventHanlder() { if(!this.event || this.event === '') return; switch(this.event) { case 'attached': //handle a user joined const { id, private_id: privateID } = this.msg; // To do something with publishers LocalRTC.setIDs(id, privateID); 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.substream) { console.log('got sub streams'); } if(this.msg.publishers) { // handle new feeds console.log('Yaaay new feeds', this.msg); } if(this.msg.leaving) { // handle a user is leaving console.log('Owww, is leaving :(', this.msg); } if(this.msg.unpublished) { console.log('someone has just shutup', this.msg); // handle a user stopped publishing } break; } } private messageErrorHandler() { if(!this.msg.error) return; console.error('ERRROR!', this.msg); if(this.msg.error_code === 429) { // Handle no room for this session console.log('Oh no, joining to an alien room', this.msg); return; } // Normal error handling console.error('Dammit!', this.msg); } private handleMessageJsep () { if(!this.jsep) return; this.handleRemoteSDP(); } }; export default SubscriberEventsHandler;