1.0.1 • Published 12 months ago

@wangct/webrtc v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

webrtc原理详解

1、创建一个rtcpeerconnection对象,参数为ice_server服务配置 const target = new webkitRTCPeerConnection(ice_server);

2、添加事件 // 发送ICE候选到其他客户端 target.onicecandidate = (event) => { if (event.candidate !== null) { //将令牌发给其他客户端 this.wsSend('rtcIceCandidate',event.candidate); } };

// 当添加流的时候 target.onaddstream = (e) => { callFunc(this.options.onStream,e.stream,e); };

3、添加本地流 target.addStream(this.local_stream);

4、接通 // 发送offer给其他客户端 target.createOffer((desc) => { // 设置本地标识,设置本地标识之后,会触发onicecandidate事件,需要将令牌发给其他人 target.setLocalDescription(desc); // 发送标识给其他人 this.wsSend('rtcOffer',desc); }, this.logErr);

// 其他人收到标识并回应 // 设置远程标识 other_target.setRemoteDescription(new RTCSessionDescription(desc)); other_target.createAnswer((desc) => { // 设置本地标识 target.setLocalDescription(desc); // 回应标识给其他人 this.wsSend('rtcAnswer',desc); }, this.logErr);

// 收到回应之后设置远程标识 target.setRemoteDescription(new RTCSessionDescription(desc));

// 收到其他人的令牌存储起来 target.addIceCandidate(new RTCIceCandidate(candidate));

1.0.1

12 months ago

1.0.0

12 months ago