无法理解为什么多个对等人无法在我的WebRTC应用程序中工作

时间:2019-02-01 23:47:22

标签: javascript sockets webrtc peer

我不明白为什么当第三对等连接时,它会为第三对等抛出此错误。我认为这可能是因为联系变得混乱,因为当第三个对等方联系时,他会立即收到两个报价 我不知道我是否应该像队列一样。虽然我不确定那是问题所在。我要去尝试了,但让我知道如果任何人都可以看到什么即时通讯做错了。 无法读取未定义的属性“ setRemoteDescription”

componentDidMount() {
    setUserMedia();
    this.socket.emit('createOrJoin', this.props.session);
    this.socket.on('signal', (data, remoteId) => {
        switch (data.type) {
            case 'newJoin':
            console.log('joined')
                this.createPeerRtcAndStream(remoteId, (rtc) => {
                    this.createOffer(rtc, (offer) => this.socket.emit('signal', offer));
                    this.remoteClients++;
                });
                break;
            case 'offer':
                this.remoteClients++;
                this.createPeerRtcAndStream(remoteId, (rtc) => {                
                        rtc.setRemoteDescription(new RTCSessionDescription(data), () => {
                            this.createAnswer(rtc, (answer) => this.socket.emit('signal', answer));
                        });                                 
                });
                break;
            case 'answer':          
                    this.rtcs[remoteId].setRemoteDescription(new RTCSessionDescription(data));
                break;
            case 'candidate':
                let hisCandidate = new RTCIceCandidate({
                    sdpMLineIndex: data.label,
                    candidate: data.candidate
                });
                if (this.rtcs[remoteId] !== undefined && this.rtcs[remoteId].remoteDescription.type) {
                    this.rtcs[remoteId].addIceCandidate(hisCandidate);
                }
                break;
            case 'clientLeft':
                this.handleLeavingClient(remoteId);
                break;
            default:
                this.socket.emit('signal', { type: 'failedConnection' });
        }
    });
}


console.log('CLIENT DESC', session);
        if(session.creatingSession===false){
            if(rooms[session.sessionKey]){
                reciever = socket.id;
                socket.join(session.sessionKey);
        rooms[session.sessionKey].clients.push(socket.id); 
                socket.in(session.sessionKey).emit('signal',{type:'newJoin'}, socket.id);
            }               
        } else {    
            if(!rooms[session.sessionKey]){
                socket.join(session.sessionKey);    
                msgs[session.sessionKey] = [];        
                rooms[session.sessionKey] = {};
                rooms[session.sessionKey].clients = [];
            }               
    rooms[session.sessionKey].name = session.room;
    rooms[session.sessionKey].clients.push(socket.id);
    console.log('CREATED SESSION');
            redClient.hset('rooms', session.sessionKey, JSON.stringify(session)) 
        }
        socket.on('signal', (data) => {
            switch (data.type){
                case 'offer':
                    console.log('initiator  recieved');
                    sender = socket.id
                    io.to(reciever).emit('signal', data, socket.id);    
                 break;
             case 'answer':
                 console.log('answer recieved');            
                 io.to(sender).emit('signal', data, socket.id); 
                 break;
             case 'candidate':
                 console.log('Recieved candidate'); 
                 sender = socket.id;     
                 if(socket.io==reciever){
                    io.to(sender).emit('signal', data, socket.id);  
                 } else {
                    io.to(reciever).emit('signal', data, socket.id);    
                 }               
                 break;
             case 'failedConnection':
                 console.log('FAILED CONNECTION')
             default :
                 console.log('NO CASE EXECUTED')    
            } 

     });

0 个答案:

没有答案