通过peer.js发送变量
Sending variable through peer.js
我正在创建一个web应用程序与peer.js。这是一个应用程序,你可以通过网络摄像头看到对方,只需要通过peer.js的服务器提交对方的ID。
它工作得很好,但我希望能够发送一个变量给另一个人。我想这样做的原因是我想创建一个按钮,当它被按下时,其他人就会听到声音。
:
$('.button').click(function()
{
var clicked = true;
//send var clicked to the other person so I can use an if statement there
}
我有两个问题。我不知道如何通过对等服务器发送这个变量给另一个人,它是由jquery完成的,所以我不确定我是否可以使用这个变量作为javascript代码的全局变量。
<script>
$( document ).ready(function() {
$('.controls').hide();
$('.bovenlijst').hide();
$('.fotomaken').hide();
});
// Compatibility shim
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// PeerJS object
var peer = new Peer({ key: 'lwjd5qra8257b9', debug: 3, config: {'iceServers': [
{ url: 'stun:stun.l.google.com:19302' } // Pass in optional STUN and TURN server for maximum network compatibility
]}});
peer.on('open', function(){
$('#my-id').text(peer.id);
console.log(peer.id);
});
// Receiving a call
peer.on('call', function(call){
// Answer the call automatically (instead of prompting user) for demo purposes
call.answer(window.localStream);
step3(call);
});
peer.on('error', function(err){
alert(err.message);
// Return to step 2 if error occurs
step2();
});
// Click handlers setup
$(function(){
$('#make-call').click(function(){
// Initiate a call!
var call = peer.call($('#callto-id').val(), window.localStream);
$('#step2').hide();
$('.controls').show();
$('.bovenlijst').show();
$('.fotomaken').show();
step3(call);
});
$('#end-call').click(function(){
window.existingCall.close();
step2();
});
// Retry if getUserMedia fails
$('#step1-retry').click(function(){
$('#step1-error').hide();
step1();
});
// Get things started
step1();
});
function step1 () {
// Get audio/video stream
navigator.getUserMedia({audio: true, video: true}, function(stream){
// Set your video displays
$('#my-video').prop('src', URL.createObjectURL(stream));
window.localStream = stream;
step2();
}, function(){ $('#step1-error').show(); });
}
function step2 () {
$('#step1, #step3').hide();
$('#step2').show();
}
function step3 (call) {
// Hang up on an existing call if present
if (window.existingCall) {
window.existingCall.close();
}
// Wait for stream on the call, then set peer video display
call.on('stream', function(stream){
$('#their-video').prop('src', URL.createObjectURL(stream));
});
// UI stuff
window.existingCall = call;
$('#their-id').text(call.peer);
call.on('close', step2);
$('#step1, #step2').hide();
$('#step3').show();
}
</script>
您所需要做的就是为媒体连接另外打开一个到另一个对等体的数据连接。这样,您就可以像在客户机之间传递变量一样传递数据。这个功能包含在PeerJS中。
来自使用变量扩展的文档:
连接:
var myVariable = 'this is a test';
var conn = peer.connect('another-peers-id');
conn.on('open', function(){
conn.send(myVariable);
});
收到
peer.on('connection', function(conn) {
conn.on('data', function(data){
// Will print 'this is a test'
console.log(data);
});
});
参见http://peerjs.com/
相关文章:
- 等待函数完成,然后将返回值分配给变量JS
- 节点中的变量.js JavaScript 不会改变
- 如何在 Node 中访问模块全局变量.js例如浏览器中的 window.variable
- 在角度中使用父范围变量.js
- 在重新声明变量 (JS) 时遇到了问题
- 在主干中使用全局变量.js .最佳实践
- 在节点中使用变量.js mysql-node 查询
- 如果变量 JS 太高,则启动/停止反转
- 变量 Js 在 PHP 代码中不起作用
- 无法使用车把正确在我的“模板”中显示变量.js
- 引导程序中的全局变量.js在 Firefox for Android 扩展中
- Webstorm 中的“未解析变量 JS”
- 变量JS的子字符串值
- 动态更新变量JS
- 不能用一个主变量访问单个变量[jS]
- 创建全局变量js
- 当不同的函数需要从数据库调用数据时,如何避免使用全局变量.(JS)
- 变量JS中的变量
- 本地存储变量js
- 为循环追加变量js