0

我正在使用 simple-peer 库。我需要将信号数据导出到 Rails,然后与活动电缆室中的任何人共享。为什么函数返回未定义,但在其中我可以打印数据?

操场

let Peer = window.SimplePeer;

navigator.mediaDevices
  .getUserMedia({
    video: false,
    audio: true,
  })
  .then(gotMedia)
  .catch(() => {});

function gotMedia(stream) {
  var peer1 = new Peer({ initiator: true, stream: stream });
  var peer2 = new Peer();

  let i = function () {
    peer1.on("signal", (data) => {
      peer2.signal(data);
      console.log(data);
      return data;
    });
  };

  console.log(i());

  peer2.on("signal", (data) => {
    peer1.signal(data);
  });
  peer2.on("stream", (stream) => {
    const audio = new Audio();
    audio.srcObject = stream;
    audio.autoplay = true;
  });
}
4

0 回答 0