max*_*127 3 javascript webrtc peerjs
我正在尝试使用 PeerJs 创建一个单向视频应用程序。我已经成功地运行了自己的对等服务器并通过单击按钮进行连接,但我无法关闭连接,以便对等点可以接收/与新对等点建立新连接。
每个用户要么是主机,要么是客户端,永远不会倒退。因此,主机可以选择连接到哪个客户端,并且客户端将开始将其摄像头反馈流回主机。closeCon()单击按钮即可调用该函数。
$(document).ready(function(){
peer = new Peer('100001', {host: 'my.url', port: '9900', path: '/peerjs', key: 'peerjs', secure: true, debug: 3});
peer.on("open", function(id) {
console.log("My peer ID is: " + id);
});
video = document.getElementById('vidSrc');
})
function callTheGuy(id){
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
getUserMedia({video: true, audio: false}, function(stream) {
window.call = peer.call(id, stream);
localStream = stream;
window.call.on('stream', function(remoteStream) {
let video = document.getElementById('vidArea');
video.srcObject = remoteStream;
video.play();
$("#videoModal").modal('show')
});
}, function(err) {
console.log('Failed to get local stream' ,err);
});
}
function closeCon(){
window.call.close();
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,我得到了我的视频,没问题。这是我的客户端代码:
peer = new Peer(serverId, {
host: "my.url",
port: "9900",
path: "/peerjs",
key: "peerjs",
debug: 3,
secure: true
});
peer.on("open", function(id) {
console.log("My peer ID is: " + id);
});
var getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
peer.on("call", function(call) {
getUserMedia(
{ video: true, audio: false },
function(stream) {
localStream = stream;
call.answer(stream); // Answer the call with an A/V stream.
},
function(err) {
console.log("Failed to get local stream", err);
}
);
call.on("close", function() {
console.log("closing");
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,当我调用 时closeCon(),客户端文件没有收到关闭事件。的部分
call.on("close", function() {
console.log("closing");
});
Run Code Online (Sandbox Code Playgroud)
永远不会被解雇。我不太确定为什么会发生这种情况,但除非关闭事件得到处理,否则客户端将保持与原始主机的连接,并且无法接受来自后续主机请求的连接。有人有建议吗?
在发现我的peerConnections继续使用chrome://webrtc-internals发送流数据后,我遇到了这个问题。我目前正在使用公共peerjs 服务器。MediaConnection不会触发该事件,但DataConnection类仍然会触发该事件。我的特定流程等待远程启动(数据)连接,然后开始调用。close
我可以通过以下方式关闭 MediaConnection:
这看起来像:
function handlePeerDisconnect() {
// manually close the peer connections
for (let conns in peer.connections) {
peer.connections[conns].forEach((conn, index, array) => {
console.log(`closing ${conn.connectionId} peerConnection (${index + 1}/${array.length})`, conn.peerConnection);
conn.peerConnection.close();
// close it using peerjs methods
if (conn.close)
conn.close();
});
}
}
peer.on('connection', conn => {
let call = peer.call(peerToCall, localStream);
// peerjs bug prevents this from firing: https://github.com/peers/peerjs/issues/636
call.on('close', () => {
console.log("call close event");
handlePeerDisconnect();
});
}
// this one works
conn.on('close', () => {
console.log("conn close event");
handlePeerDisconnect();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6283 次 |
| 最近记录: |