const video = document.querySelector('video');
let client = {};
var stream;
navigator.mediaDevices.getUserMedia({ video: true, audio: true }, function (stream) {
stream = stream;
}, function () { })
.then(stream => {
connection.invoke('NewVideoClient');
video.srcObject = stream
video.play();
function InitPeer(type) {
var peer = new SimplePeer({
initiator: (type == 'init') ? true : false,
stream: stream,
trickle: false
});
peer.on('stream', function (stream) {
CreateVideo(stream);
});
peer.on('connect', () => {
console.log('CONNECT');
alert('CONNECT');
});
//peer.on('close', function () {
// document.getElementById('peerVideo').remove();
// peer.destroy();
//});
return peer;
}
// for peer type init
function CreatePeer() {
alert("Enter in to CreatePeer");
client.gotAnswer = false;
let peer = InitPeer('init');
// let data = null;
peer.on('signal', function (data) {
if (!client.gotAnswer) {
connection.invoke('SendOffer', data);
alert(JSON.stringify(data));
}
});
client.peer = peer;
}
//peer not init to pass the answer
function CreateAnswer(offer) {
let peer = InitPeer('notInit');
let data = null;
peer.on('signal', (offer) => {
alert("Enter in to CreateAnswer");
connection.invoke('SendAnswer', offer);
});
peer.signal(offer);
}
//this fuction will run when answer send from server
function SignalAnswer(answer) {
alert("Enter in to Signaling");
client.gotAnswer = true;
let peer = client.peer;
peer.signal(answer);
};
//this function will create video
function CreateVideo(stream) {
alert("video play");
let video = document.createElement('video');
video.id = 'peerVideo';
video.style.width = '400px';
video.style.height = '400px';
video.srcObject = stream;
video.class = 'embed-responsive-item';
document.querySelector("#peerDiv").appendChild(video);
video.play();
}
function SessionActive() {
document.write("User is in another call.Please come back");
}
//Hub will call this function to create the offer
connection.on("CreateOffer", function () {
alert("Enter in to CreateOffer");
CreatePeer();
});
//Hub will call this method to create the answer
connection.on("CreateAnswer", function (offer) {
alert("Enter in to CreateAnswer");
CreateAnswer(offer);
});
//Hub will call this fucntion to send the answer ot the caller
connection.on("SignalAnswer", function (answer) {
alert("Enter in to Signaling");
SignalAnswer(answer);
})
//Hub will call this fuction to this connect the video
connection.on("RemovePeer", function () {
document.getElementById('peerVideo').remove();
if (client.peer) {
client.peer.destroy();
}
});
})
.catch(err => document.write(err));
I have written 3 methods in my video chat application as I new to javascript there is a small confusion. In method "SignalAnswer" it call "peer.signal(answer)" and this calls to the signal function inside "CreatePeer". Inside "CreateAnswer" method also there is a signal function created but it is not called. it will be call by peer.signal(offer) in side the same method. My guess is because peer will get two different objects in two different time. So according to the object assign this function will be called.Can any one explain about this. how these functions are working in javascripts NOTE : Connection is used to call the SingnalR hub.