记vue websocket+webRTC实现1v1视频通话

页面上俩个窗口


<video id="localVideo" style="width: 320px; height: 240px;" autoplay muted></video>
<video id="remoteVideo" style="width: 320px; height: 240px;" autoplay muted></video>

此处是在一进来就在mounted建立了,可自行根据代码逻辑调整位置

ws = new WebSocket("ws://192.168.0.xxx:9000");
    // 推流用的MediaStream
pc = new RTCPeerConnection();
let that = this;
//此处监听 而后发送信息
pc.addEventListener("icecandidate", event => {
      if (event.candidate) {
        ws.send(
          JSON.stringify({
            type: iceType,
            candidate: event.candidate
          })
        );
      }
});


pc.addEventListener("track", event => {
  that.remoteVideo.srcObject = event.streams[0];
});

打开摄像头(手动调用)

 async hanldeJoin() {
      this.show = false;
      if (!this.roomId) {
        this.$message.warning("请输入房间号");
        return;
      }


      this.caller = true;
      this.calling = true;

      let that = this;
      this.$nextTick(() => {
        that.localVideo = document.getElementById("localVideo");
        that.remoteVideo = document.getElementById("remoteVideo");
      });
      try {
        stream = await navigator.mediaDevices.getUserMedia({
          audio: false,
          video: true
        });
        //创建媒体录制对象
        // that.recorder = new window.MediaRecorder(stream);
       // that.bindEvents();

        that.localVideo.srcObject = stream;
        that.localVideo.play();
        that.localStream = stream;
       // 房间号参数自行调整 1v1未带入,连入同一个websocket即可
        that.doJoin(that.roomId);
      } catch (err) {
        that.$message.warning("无法访问用户媒体设备");
        console.error(err); // 无法访问用户媒体设备
        alert(err);
      }
    },
 async doJoin(roomId) {
     
        ws.send(
         JSON.stringify({
           type: "connect"
         })
       );
 // 收到服务端消息
	ws.onmessage = function(evt) {

 	if (typeof evt.data == "string") {
     var str = evt.data;
      if (str.length <= 0) {
        return;
    } else {
       //处理其他请求
     let event = JSON.parse(str);
     switch (event.type) {
       case "connect":
         console.log("连接成功,等待其他用户");
          break;

       case "create_offer":
          that.sendOffer();
           break;

       case "offer":
         that.caller = false;
         that.recvOffer(event);
         break;

       case "answer":
          that.caller = false;
          that.recvAnswer(event);
         break;

       case "offer_ice":
      case "answer_ice":
         pc.addIceCandidate(event.sdp);
         break;

      default:
         break;
      }
   }
  }
 };
    },

rtc的传递

 sendOffer() {
      iceType = "offer_ice";
      pc.addTrack(this.localStream.getVideoTracks()[0], this.localStream);
      pc.createOffer({
        offerToReceiveAudio: true,
        offerToReceiveVideo: true
      }).then(offer => {
        pc.setLocalDescription(offer).then(() => {
          ws.send(JSON.stringify(offer));
        });
      });
    },
    recvOffer(offer) {
      iceType = "answer_ice";
      pc.addTrack(this.localStream.getVideoTracks()[0], this.localStream);
      pc.setRemoteDescription(offer).then(() => {
        pc.createAnswer().then(answer => {
          pc.setLocalDescription(answer).then(() => {
            ws.send(JSON.stringify(answer));
          });
        });
      });
    },
    recvAnswer(answer) {
      pc.setRemoteDescription(answer);
    },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用VueWebRTC实现音视频通话,您可以使用WebRTC API。以下是一些步骤: 1. 首先,您需要创建一个新的Vue项目,您可以使用Vue CLI来创建。 2. 接下来,您需要使用WebRTC API来实现音视频通话。在Vue中,您可以使用Vue插件来轻松地使用WebRTC API。例如,您可以使用vue-webrtc插件。 3. 您需要在Vue组件中使用vue-webrtc插件。您可以在组件的模板中放置一个video标签,该标签将用于显示视频流。使用vue-webrtc插件,您可以轻松地与其他用户进行音视频通话。 4. 在Vue组件中,您可以使用WebRTC API来处理音视频流。您可以使用getUserMedia()函数获取本地视频流,并使用RTCPeerConnection对象将视频流发送到远程用户。使用RTCPeerConnection对象,您可以建立点对点连接,并在WebRTC流之间进行通信。 5. 最后,您需要使用Vue的事件来处理音视频通话中的各种情况,例如在连接断开时显示错误消息等。 这是一个简单的示例代码,演示如何使用VueWebRTC API实现音视频通话: ``` <template> <div> <video ref="localVideo" autoplay></video> <video ref="remoteVideo" autoplay></video> </div> </template> <script> import VueWebRTC from 'vue-webrtc' export default { name: 'VideoChat', components: { VueWebRTC }, data () { return { localStream: null, remoteStream: null, peerConnection: null } }, mounted () { this.startLocalVideo() }, methods: { startLocalVideo () { navigator.getUserMedia({ video: true, audio: true }, (stream) => { this.localStream = stream this.$refs.localVideo.srcObject = stream this.setupPeerConnection() }, (error) => { console.error(error) }) }, setupPeerConnection () { this.peerConnection = new RTCPeerConnection() this.peerConnection.onicecandidate = (event) => { if (event.candidate) { // send candidate to remote user } } this.peerConnection.onaddstream = (event) => { this.remoteStream = event.stream this.$refs.remoteVideo.srcObject = event.stream } this.peerConnection.addStream(this.localStream) // create offer and send to remote user } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值