rtc.js 2.4 KB

1
  1. import Socket from"./socketEvent";import EVENT from"./EVENT";import{Base64}from"../utils/js-base64";const version="v0.7.0",TAG_NAME="RTC";class RTC extends Socket{async getJoinChannelTokenParams(){const e=this.user;if(!e.sessionId)try{await this.setSessionId()}catch(e){console.warn(e)}const s=wx.getSystemInfoSync(),t=Date.now();return{channelId:this.roomInfo.roomId,timestamp:t,uid:e.userId,userId:e.userId,scene:"student"===e.userType?"alone":"ppt",userType:"audience",viewerId:e.userId,nickname:Base64.encodeURI(e.nick),client:s.platform,clientVersion:s.version,clientTs:t,sessionId:e.sessionId,deviceType:"",model:Base64.encodeURI(s.model),os:s.system,osVersion:s.SDKVersion,networkType:"",screenWidth:s.screenWidth,screenHeight:s.screenHeight}}constructor(e){super(e),this.joinUserInfo={},this.rtcConnected=!1,this.successToken=null,this.disableRtcCloseEvent=e.disableRtcCloseEvent||!1,this.socketEventListener()}async initLocalStream(e=!1){this.trigger(EVENT.INIT_LOCAL_STREAM_READY,{init:async e=>{this.emitSocketMessage("joinSuccess",(e=>{try{const s=JSON.parse(e);this.successToken=s.token}catch(e){console.warn(e)}}))},type:this.roomInfo.type,streamId:this.user.userId,master:!1,nick:this.user.nick,pic:this.user.nick})}socketEventListener(){this.on(EVENT.CLIENT_BANNED,(()=>{this.leaveChannel()})),this.on(EVENT.MUTEUSER_VIDEO,(e=>{e.mute?this.disableVideo():this.enableVideo()})),this.on(EVENT.MUTEUSER_AUDIO,(e=>{e.mute?this.disableAudio():this.enableAudio()})),this.on(EVENT.ALLOW_MICROPHONE,(()=>{this.rtcConnected||this.initLocalStream()})),this.on(EVENT.LOGIN,(()=>{this.successToken&&this.socket.emit("reJoinMic",this.successToken)})),this.disableRtcCloseEvent||this.on(EVENT.CLOSE_MICROPHONE,(()=>{this.leaveChannel()}))}joinChannel(e){super.emitSocketMessage("joinRequest",(s=>{this.applyStatus=!0,"function"==typeof e&&e(s)}))}cancelJoinChannel(e){super.emitSocketMessage("joinLeave",(s=>{this.applyStatus=!1,"function"==typeof e&&e(s)}),this.successToken),this.successToken=void 0}async leaveChannel(){this.applyStatus=!1,this.rtcConnected=!1,this.cancelJoinChannel(),this.trigger(EVENT.LEAVE_CHANNEL_SUCCESS)}disableVideo(){this.trigger(EVENT.LOCAL_MUTE_VIDEO,{})}enableVideo(){this.trigger(EVENT.LOCAL_UNMUTE_VIDEO,{})}disableAudio(){this.trigger(EVENT.LOCAL_MUTE_AUDIO,{})}enableAudio(){this.trigger(EVENT.LOCAL_UNMUTE_AUDIO,{})}destroy(){this.rtcConnected&&this.leaveChannel(),this.removeSocketListener()}}export default RTC;