1

i am trying to build one-to-one video chatroom with socket.io and simple-peer, it works as it should for only 2 persons and if third one connects code alerts him that there are already 2 person and s/he should wait. and thing i want to do is to add rooms for only 2 persons (for example first couple enters to room number 1, second enters to room number 2 and etc.)to this code for giving everyone ability to have video chat with each other without waiting. it tried this code but it did not give me result i wanted

let room = vnumb //vnumb is global variable which determines room name;
socket.join(room);
socket.current_vroom = room
vchns.in(room).clients((err,clients)=>{
if(err) console.log(err);

if(clients.length < 2){
  if(clients.length == 1){
  //emit some event

  }
}
if(clients.length >= 2){
  ++vnumb
}
})

here is server code

let clients = 0

io.on('connection', function (socket) {
    socket.on("NewClient", function () {
        if (clients < 2) {
            if (clients == 1) {
                this.emit('CreatePeer')
            }
        }
        else
            this.emit('SessionActive')
        clients++;
    })
    socket.on('Offer', SendOffer)
    socket.on('Answer', SendAnswer)
    socket.on('disconnect', Disconnect)
})

function Disconnect() {
    if (clients > 0) {
        if (clients <= 2)
            this.broadcast.emit("Disconnect")
        clients--
    }
}

function SendOffer(offer) {
    this.broadcast.emit("BackOffer", offer)
}

function SendAnswer(data) {
    this.broadcast.emit("BackAnswer", data)
}

so, how can i do what i want? Thanks!

4

0 回答 0