i have #client.js file
const socket=io("/");
var userid=Math.floor(Math.random()*100000);
socket.emit("user-joined",userid);
var localvideo,remotevideo,remotestream,localstream;
localvideo=document.getElementById("local");
remotevideo=document.getElementById("remote");
async function init(){
const server={"iceserver":[{"urls":['stun:stun1.1.google.com','stun:stun2.1.google.com']}]}
const peerconnection=new RTCPeerConnection(server)
localstream=await navigator.mediaDevices.getUserMedia({video:true,audio:false});
localvideo.srcObject=localstream;
localstream.getTracks().forEach((track)=>{
peerconnection.addTrack((track),localstream);
})
remotestream=new MediaStream();
peerconnection.ontrack=e=>{
e.streams[0].getTracks().forEach((track)=>{
remotestream.addTrack((track));
remotevideo.srcObject=remotestream;
})
}
peerconnection.onicecandidate=(e)=>{
if(e.candidate){
send({
type:"candidate",
candidate:e.candidate,
name:userid
});
console.log(e.candidate)
}
}
let offer=await peerconnection.createOffer()
await peerconnection.setLocalDescription(offer)
send({
type:"offer",
offer:offer,
name:userid
});
socket.on("message",async(message)=>{
var data={};
try {
data=JSON.parse(message)
} catch (error) {
console.log("invalid json")
data={};
}
switch(data.type){
case "candidate":
if(userid!=data.sender){
peerconnection.addIceCandidate(new RTCIceCandidate(data.candidate))
}
break;
case "offer":
if(userid!=data.name){
peerconnection.setRemoteDescription(new RTCSessionDescription(data.offer));
let answer=await peerconnection.createAnswer()
await peerconnection.setLocalDescription(answer);
send({
type:"answer",
answer:answer,
name:data.sender
})
}
break;
case "answer":
if(userid===data.sender){
peerconnection.setRemoteDescription(new RTCSessionDescription(data.answer))
}
}
})
}
init()
function send(message){
socket.emit("message",JSON.stringify(message));
}
server.js code am trying to make the code work in the way that if only one user has joined the room,no offer or candidate flows and on second user joining offers and candidates flow.i have tried this repeatedly but i cant see my remote video coming up. i don`t know what to do help bros and sis. am just a newbie so much in need of knowing #webrtc
const express=require('express');
const app=express();
const server=require('http').createServer(app);
const io=require('socket.io')(server);
const {v4:uuidv4}=require('node-uuid');
var users=[];
app.set('view engine','ejs');
app.use(express.static('public'));
app.get('/',(req,res)=>{
res.redirect('/'+uuidv4());
})
app.get('/:room',(req,res)=>{
res.render('room',{roomId:req.params.room})
})
io.on("connection",connection=>{
connection.on("user-joined",userid=>{
console.log("user: "+userid+" joined room");
users.push(userid)
console.log("users:",users)
if(users.length>1){
connection.on("message",message=>{
var data={};
try {
data=JSON.parse(message)
} catch (error) {
console.log("invalid json",error);
data={};
}
switch(data.type){
case "candidate":
for(let i=0;i<users.length;i++){
if(data.name!=users[i]){
send({
type:"candidate",
candidate:data.candidate,
sender:data.name
})
console.log("candidates sent")
}
}
break;
case "offer":
for(let i=0;i<users.length;i++){
if(data.name!=users[i]){
send({
type:"offer",
offer:data.offer,
sender:data.name
})
console.log("offer sent")
}
}
break;
case "answer":
send({
type:"answer",
answer:data.answer,
sender:data.name
})
break;
}
})
}
})
})
function send(message){
io.emit("message",JSON.stringify(message));
}
server.listen(3000,()=>{
console.log("server listening at port 3000")
});
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.