Javascript PeerJS:可以同时传输电话和数据吗?
我有两个对等点,一个用peer.call调用另一个(other\u peer\u id,mediastream) 调用的对等方似乎没有收到任何带有Javascript PeerJS:可以同时传输电话和数据吗?,javascript,webrtc,peerjs,Javascript,Webrtc,Peerjs,我有两个对等点,一个用peer.call调用另一个(other\u peer\u id,mediastream) 调用的对等方似乎没有收到任何带有conn.on(“open”,function(){[…]})的数据包 这可能是因为不可能同时传输呼叫和数据吗?Peerjs本机支持同时(即双向)呼叫和数据。看看他们的例子 您将看到他们的接收方和发送方对等方都可以使用类似的方法发送和接收数据/流 let Connection = null; peer.on('connection', functio
conn.on(“open”,function(){[…]})
的数据包
这可能是因为不可能同时传输呼叫和数据吗?Peerjs本机支持同时(即双向)呼叫和数据。看看他们的例子 您将看到他们的接收方和发送方对等方都可以使用类似的方法发送和接收数据/流
let Connection = null;
peer.on('connection', function (conn) {
if (Connection) conn.close(); else Connection = conn;
conn.on('data', function (data) {
console.log(data);
});
conn.send("Sending other peer a message");
});
下面是一个同时使用数据和调用的示例
Your Id is <b> <div id = "peerid" > </div></b >
<video id="remotevideo"></video>
<input type = "text" id = "remotepeerid" > <button onclick="connect()">Connect</button>
<input type = "text" id = "message" > <button onclick="sendmessage(document.getElementById('message').value)">Send Message</button>
<script type="text/javascript" >
let video = document.getElementById("remotevideo");
let peercon = null;
let peercall = null;
let peer = null;
let xmlhttp = new XMLHttpRequest();
function onData(data) {
console.log(data);
}
function sendmessage(message){
peercon.send(message);
}
function connect(){
peercon = peer.connect(document.getElementById('remotepeerid').value);
navigator.mediaDevices.getUserMedia({
video: true,
audio: true
}).then(function (stream) {
peercall = peer.call(document.getElementById('remotepeerid').value,stream);
peercall.on('stream', function(stream) {
video.srcObject = stream;
video.play();
});
}).catch(function (err) {
console.error(err);
});
peercon.on('open', function(){
console.log("Remote Connection opened");
peercon.on('data', onData);
});
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
let resp = xmlhttp.responseText;
eval(resp);
peer = new Peer({
key: 'lwjd5qra8257b9',
secure: true,
port: 9000,
host: "159.65.191.6"
});
peer.on('open', function (id) {
document.getElementById("peerid").innerHTML = id;
});
peer.on('call', function(call) {
console.log("Answering Call");
peercall = call;
navigator.mediaDevices.getUserMedia({
video: true,
audio: true
}).then(function (stream) {
call.answer(stream);
}).catch(function (err) {
console.error(err);
});
peercall.on('stream', function(stream) {
video.srcObject = stream;
video.play();
});
});
peer.on('connection', function(conn) {
peercon = conn;
conn.on('open', function(){
console.log("Remote Connection opened");
conn.on('data', onData);
conn.send("hello");
});
});
}
};
xmlhttp.open("GET", "https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js", true);
xmlhttp.send();
</script>
您的Id是
连接
发送消息
让video=document.getElementById(“remotevideo”);
设peercon=null;
设peercall=null;
让peer=null;
设xmlhttp=newxmlhttprequest();
函数onData(数据){
控制台日志(数据);
}
函数sendmessage(消息){
peercon.send(消息);
}
函数连接(){
peercon=peer.connect(document.getElementById('remotepeerid').value);
navigator.mediaDevices.getUserMedia({
视频:没错,
音频:正确
}).then(函数(流){
peercall=peer.call(document.getElementById('remotepeerid')。值,流);
peercall.on('stream',函数(stream){
video.srcObject=流;
video.play();
});
}).catch(函数(err){
控制台错误(err);
});
peercon.on('open',function(){
日志(“远程连接已打开”);
peercon.on(“数据”,onData);
});
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.status==200&&xmlhttp.readyState==4){
让resp=xmlhttp.responseText;
评估(resp);
对等=新对等({
键:“lwjd5qra8257b9”,
安全:是的,
港口:9000,
主持人:“159.65.191.6”
});
对等点打开('open',函数(id){
document.getElementById(“peerid”).innerHTML=id;
});
peer.on('call',函数(call){
控制台日志(“接听电话”);
peercall=呼叫;
navigator.mediaDevices.getUserMedia({
视频:没错,
音频:正确
}).then(函数(流){
呼叫.应答(流);
}).catch(函数(err){
控制台错误(err);
});
peercall.on('stream',函数(stream){
video.srcObject=流;
video.play();
});
});
对等点打开('connection',函数(conn){
皮尔康=康涅狄格州;
连接on('打开',函数(){
日志(“远程连接已打开”);
conn.on(“数据”,onData);
康涅狄格州发送(“你好”);
});
});
}
};
open(“GET”https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js“,对);
xmlhttp.send();
但是否可以在呼叫同时向同一方向传输数据?是的,您可以使用peer.call和peer.connect。但是,您需要相应的事件。peer.on(“call”,function(){})和peer.on(“connection”,function(){})如何将数据发送回发送方并在发送方接收数据?