我正在尝试使用socket.io和simple-peer建立一对一的视频聊天室,它仅适用于2个人,并且如果第三个人连接代码会提醒他已经有2个人和s /他应该等待。我想做的是仅添加2个人的房间(例如,第一对夫妇进入房间1,第二个进入房间2等),以使每个人都可以彼此之间进行视频聊天等候。它尝试了这段代码,但没有给我想要的结果
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
}
})
这是服务器代码
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)
}
那么,我该怎么办?谢谢!