UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

2 lines (1 loc) 3.93 kB
"use strict";var e=Object.defineProperty,t=(t,s,i)=>(((t,s,i)=>{s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[s]=i})(t,"symbol"!=typeof s?s+"":s,i),i);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class s{constructor(e,s){t(this,"host"),t(this,"port"),t(this,"userId"),t(this,"isReady"),t(this,"messageList"),t(this,"icecandidateList"),t(this,"handlerMap"),t(this,"socket"),t(this,"peer"),this.host=e,this.port=s,this.userId=0,this.isReady=!1,this.messageList=[],this.icecandidateList=[],this.handlerMap={},this.socket=null,this.peer=null,this.init()}static checkSupport(){return!!(RTCPeerConnection||webkitRTCPeerConnection||mozRTCPeerConnection)}static createPeer(){return new(RTCPeerConnection||webkitRTCPeerConnection||mozRTCPeerConnection)({iceServers:[{urls:["stun:stun.xten.com","stun:stun.l.google.com:19302"]}]})}static createIceCan(e){return new(RTCIceCandidate||webkitRTCIceCandidate||mozRTCIceCandidate)(e)}static createDescript(e){return new(RTCSessionDescription||webkitRTCSessionDescription||mozRTCSessionDescription)(e)}init(){const e=`wss://${this.host}${this.port?`:${this.port}`:""}/webrtc?password=123`,t=new WebSocket(e);this.socket=t,t.onopen=this.onOpen.bind(this),t.onmessage=this.onMessage.bind(this),t.onclose=this.onClose.bind(this),t.onerror=this.onError.bind(this)}initPeer(e){const t=s.createPeer();this.peer=t,this.initPeerBind(e)}initPeerBind(e){const t=this.peer;t.onconnectionstatechange=this.onPeerConnectionChange.bind(this,e),t.onicecandidate=this.onPeerIceCandidate.bind(this,e),t.ontrack=this.onPeerAddTrack.bind(this,e)}onPeerConnectionChange(e,t){this.debug("peer connection change",this.peer.connectionState);let s=0;"connecting"===this.peer.connectionState&&(s=1),"connected"===this.peer.connectionState&&(s=2,this.excuteHandler("connected",{})),"disconnected"===this.peer.connectionState&&(s=0,this.excuteHandler("disconnected",{})),this.send({event:"status",userId:this.userId,data:{hasConnection:s}})}onPeerAddTrack(e,t){this.excuteHandler("track",t)}onPeerIceCandidate(e,t){if(t.candidate&&"udp"==t.candidate.protocol){const s=t.candidate,i={event:"ice",userId:this.userId,toUserId:e.fromUserId,data:s};this.send(i)}}onOpen(){this.isReady=!0}onMessage(e){const{data:t}=e,s="string"==typeof t?JSON.parse(t):t,i=this[s.event];return 500===s.status?this.debug("fail",s.msg):"connection"===s.event?(this.userId=Number(s.userId)||0,this.send({event:"info",userId:this.userId,data:{userName:"SDA",desc:"SDA组件",type:"window",canP2P:!1}}),void this.excuteHandler("connection",{})):i?void i.call(this,s):this.debug("fail","处理函数不存在")}onClose(){this.isReady=!1}onError(e){this.debug("error",e)}publisher(e){const{userId:t,toUserId:s}=e,i={event:"subscriber",userId:this.userId,toUserId:s};this.peer&&(this.peer.close(),this.peer=null),this.initPeer({userId:t,fromUserId:s}),this.send(i)}async offer(e){const{data:t,toUserId:i}=e,n=s.createDescript(t);let r,o;if(n)try{for(await this.peer.setRemoteDescription(n),r=await this.peer.createAnswer(),await this.peer.setLocalDescription(r),this.send({event:"answer",userId:this.userId,toUserId:i,data:this.peer.localDescription}),o=this.icecandidateList.shift();o;){const e=s.createIceCan(o);this.peer.addIceCandidate(e),o=this.icecandidateList.shift()}}catch(a){this.debug("offer",a)}}ice(e){const{data:t}=e;if(this.peer.remoteDescription){const e=s.createIceCan(t);this.peer.addIceCandidate(e)}else this.icecandidateList.push(t)}excuteHandler(e,t){const s=this.handlerMap[e];if(s)for(let i=0;i<s.length;i++){(0,s[i])(t)}}debug(e,t){}send(e){const t=JSON.stringify(e);if(!this.isReady)return this.messageList.push(t);this.socket.send(t)}on(e,t){this.handlerMap[e]||(this.handlerMap[e]=[]),this.handlerMap[e].push(t)}off(e,t){const s=this.handlerMap[e];let i;s&&(i=s.findIndex((e=>e==t)),i>=0&&s.splice(i,1),s.length||delete this.handlerMap[e])}}exports.default=s;