javascript - 通过PeerJS接收数据

标签 javascript jquery peerjs

我正在开发一个小型 PeerJS 项目,我有点困惑。我能够打开连接并确认客户端已连接,但我无法找出接收任何内容的正确方法或语法。或者也许我只是错过了一些简单的事情。我尝试了很多变体试图让它发挥作用。下面是 JS 和 HTML。

JS:

var peer = new Peer({
  key: 'lwjd5qra8257b9'
// I use my own API key for testing, this is the public PeerJS key
//you'll have to generate your own if this doesn't work.
});

peer.on('open', function(id) {
  $('body').append('<br/>Your personal peer ID is: ' + id + '<br/>Be careful who you share it with.');
});
peer.on('connection', connect);

function connect(succ) {
  conn = succ;
  //input changes on successful connect
  $('#peerKeyEnter').val(conn.peer);
  $('#peerKeyEnter').prop('disabled',true);
}

$(document).ready(function() {

  $('#peerKeySubmit').on('click', function() {
    var buddy = document.getElementById('peerKeyEnter').value;
    var buddyConn = peer.connect(buddy); //send connection request
    connect(buddyConn); //connect to peer

    //sending data
    buddyConn.on('open', function() {
      buddyConn.send('this is supposed to work')
    });

    //receiving data
    buddyConn.on('connection', function(conn) {
      buddyConn.on('data', function(data) {
        console.log(data);
      });
    });
  }); //end peerKeySubmit.click
}); //end doc.ready()

HTML:

<html>
<head>
  <script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>
  <!--script src='peer.js'></script-->
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
  <script src='app.js'></script>
</head>
<body>

<input type='text' id='peerKeyEnter' placeholder="Enter peer's ID"></input>
<button id='peerKeySubmit' value='Send'>Select Peer</button>

</body>
</html>

最佳答案

事实上,你就快到了。您的 buddyConn.on('data'... 应该位于 connect 函数中,使用该函数的参数名称进行连接。此外,对 connect 的第二次调用 不应出现在 #peerKeySubmit.on 回调函数中。以下是修改后的(工作)代码:

var peer = new Peer({
  key: 'lwjd5qra8257b9'
// I use my own API key for testing, this is the public PeerJS key
//you'll have to generate your own if this doesn't work.
});

peer.on('open', function(id) {
  $('body').append('<br/>Your personal peer ID is: ' + id + '<br/>Be careful who you share it with.');
});
peer.on('connection', connect);

function connect(succ) {
  conn = succ;
  conn.on('data', function (data) {
    console.log('Received from ' + conn.peer + ': ' + data);
  });

  //input changes on successful connect
  $('#peerKeyEnter').val(conn.peer);
  $('#peerKeyEnter').prop('disabled',true);
}

$(document).ready(function() {

  $('#peerKeySubmit').on('click', function() {
    var buddy = document.getElementById('peerKeyEnter').value;
    var buddyConn = peer.connect(buddy); //send connection request

    //sending data
    buddyConn.on('open', function() {
      buddyConn.send('this is supposed to work')
    });

  }); //end peerKeySubmit.click
}); //end doc.ready()

关于javascript - 通过PeerJS接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44402013/

相关文章:

cordova - 如何在phonegap应用程序中传输音频

javascript - 将数组中每个单词的第一个字母大写

javascript - 数据表未发布所有选定的项目

javascript - 获取格式化为 2011-04-01 的 Javascript 日期

javascript - 从 node.js 后端控制前端 javascript

webrtc - 检测 MediaStreamTrack 是否为黑色/空白

javascript - 清除间隔不起作用

javascript - PHP字符串转json数组

javascript - 在 Javascript 中三次错误尝试后触发重定向到表单提交页面

javascript - Chrome 不会通过 WebRTC/Peer.js 播放 WebAudio getUserMedia