gpt4 book ai didi

javascript - WebRTC 视频聊天

转载 作者:行者123 更新时间:2023-11-29 22:02:53 30 4
gpt4 key购买 nike

我正在尝试使用 webrtc 和 RTCPeerConnection API 建立一对一的视频聊天。我的代码的一个问题是,在初始用户连接到服务器后,当其他用户通过 socket.io 发出消息时,它不会从服务器接收消息。客户端只接收他们自己发出的消息。这是我的一些代码。完整项目在 Github 上:https://github.com/rashadrussell/webrtc_experiment

客户端

var isInitiator = false;


socket.on('initiatorFound', function(data) {
isInitiator = data.setInitiator;
console.log("Is Initiator? " + isInitiator);
});


navigator.getMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia
);

navigator.getMedia(
{video: true, audio: false},
function(stream) {
var video = document.getElementById("localView");
video.src = window.URL.createObjectURL(stream);
console.log("Add Stream");
sendMessage('streamAdd', {streamAdded: 'stream-added'});

createPeerConnection();
pc.addStream(stream);

if(isInitiator)
{
callPeer();
}

},
function(err) {
console.log("The following error occured: ");
console.dir(err);
}

);


function sendMessage(type, message)
{
console.log("Sending Message");
socket.emit('message',{
"type": type,
"message": message
});
}

function createPeerConnection() {

pc = new rtcPeerConnection(servers, options);
console.dir(pc);

pc.onicecandidate = function(evt) {
if(evt.candidate == null) return;
pc.onicecandidate = null;

console.log("Send Ice Candidate");
sendMessage("iceCandidate", JSON.stringify(evt.candidate));
};

pc.onaddstream = function(evt) {
document.body.append("<video id='remoteVideo' autoplay></video>");
var remoteVid = document.getElementById("remoteVideo");
remoteVid.src = window.URL.createObjectURL(evt.stream);
};

}


function callPeer() {

pc.createOffer(function (offer) {
pc.setLocalDescription(offer, function() {
sendMessage("offer", JSON.stringify(offer));
});
console.log("Send Offer");
}, function(err) { console.log("Offer Error: " + err) },
videoConstraints
);

}

function answerPeer() {

pc.createAnswer(function(answer) {
pc.setLocalDescription(answer);
sendMessage("answer", JSON.stringify(answer))
}, function(err) { console.log("Sending Answer Error: " + err) },
videoConstraints
);

}

socket.on('message', function(message) {
console.log("CONSOLE MESSAGE:");
console.dir(message);

if(message.type == 'streamAdd') {
console.log('Stream was added');
createPeerConnection();

if(isInitiator) {
callPeer();
}

} else if(message.type == 'offer') {

pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message)));

if(!isInitiator)
{
console.log("Sending Answer");
answerPeer();
}


} else if(message.type == 'answer') {
pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message)));
} else if(message.type == 'iceCandidate') {
console.log("Get Ice Candidate");
pc.addIceCandidate(new rtcIceCandidate(JSON.parse(message.message)) );
}

});

服务器端

var isInitiator = false;
io.sockets.on('connection', function(socket) {

if (!isInitiator) {
isInitiator = true;
socket.emit('initiatorFound', {setInitiator: isInitiator});
} else {
socket.emit('initiatorFound', {setInitiator: !isInitiator});
}

// Signaling Channel
socket.on('message', function(message) {

if (message.type == 'streamAdd') {
console.log('Got message: ' + message);
}
//socket.emit('message' ,message);
// Should be:
socket.broadcast.emit('message', message);

});

});

最佳答案

看看你是否想向设置唯一 ID(socket.id) 的特定用户发送消息,但现在你只是尝试测试正确,所以更改你的服务器端代码

var isInitiator = false;

io.sockets.on('connection', function(socket) {

if (!isInitiator) {
isInitiator = true;
socket.emit('initiatorFound', {setInitiator: isInitiator});
} else {
socket.emit('initiatorFound', {setInitiator: !isInitiator});
}

// Signaling Channel
socket.on('message', function(message) {

if (message.type == 'streamAdd') {
console.log('Got message: ' + message);
}
socket.emit('message' ,message);

});

});

看这里 socket.emit('message',message);此套接字对象包含您的 ID,因此如果您想向您知道 unique.id 的特定用户发送消息,它会向您发送消息

io.sockets.on('connection', function(socket) {

if (!isInitiator) {
isInitiator = true;
socket.emit('initiatorFound', {setInitiator: isInitiator});
} else {
socket.emit('initiatorFound', {setInitiator: !isInitiator});
}

// Signaling Channel
socket.on('message', function(message) {

if (message.type == 'streamAdd') {
console.log('Got message: ' + message);
}
socket.broadcast.emit('message' ,message);

});

});

关于javascript - WebRTC 视频聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622478/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com