1

我正在尝试在服务器上的两个不同用户之间建立连接。每次用户加入房间时,他都会获取房间中其他用户的 socket.ids 信息。他们尝试做的是新用户创建一个发起者对等点并发送一个信号。另一个对等点出于某种原因接受了它们,它们都不断地相互发送请求,并且没有人访问彼此的请求,这就是正在发生的事情。 后端控制台的图片

createPeer = (userToSignal, callerId, stream) => {
    console.log('create peer called', userToSignal);
    const peer = new Peer({
        initiator: true,
        trickle: false,
        stream: stream
    })

    peer.on('signal', signal => {
        console.log('sending a signal to server')
        this.socket.emit('sending signal', {
            'userToSignal': userToSignal,
            'callerId': callerId,
            'signal': signal
        });
    });

    return peer;
}

addPeer = (incomingSignal, callerId, stream) => {
    console.log('Add peer called', callerId);
    const peer = new Peer({
        initiator: false,
        trickle: false,
        stream: stream
    });

    peer.signal(JSON.stringify(incomingSignal));

    peer.on('signal', signal =>{
        console.log('returning a signal to : ', callerId)
        this.socket.emit('returning signal', {
            'signal': signal,
            'callerId': callerId
        });
    });

    return peer;
}

joinCall = () => {
    navigator.mediaDevices.getUserMedia({ video: false, audio: true }).then(stream => {
        console.log(this.socket.id);
        this.socket.emit('join call', this.state.id, this.state.userName);
        console.log('Join Call Called')
        this.socket.on('all users', (usersInThisRoom) => {
            console.log('all users ', usersInThisRoom);
            const peers = [];
            usersInThisRoom.forEach(user => {
                console.log('for each called')
                const peer = this.createPeer(user['id'], this.socket.id, stream);
                peers.push({
                    'peerId' : user['id'],
                    'peer': peer 
                })
            });
            this.peersRefs = peers;
            this.setState({peersRefs : this.peersRefs});
        });

        this.socket.on('user joined', payload => {
            console.log('new user has joined', payload['callerId']);
            const peer = this.addPeer(payload['signal'], payload['callerId'], stream);
            this.peersRefs.push({
                'peerId': payload['callerId'],
                'peer': peer
            })
            this.setState({'peersRefs': this.peersRefs});
        });

        this.socket.on('receiving returned signal', payload => {
            console.log('returned signal recieved ', payload.id)
            const peerToSignal = this.peersRefs.find(peer => peer['peerId'] === payload.id)
            peerToSignal['peer'].signal(JSON.stringify(payload.signal));
        });
    });

    console.log('joining')     
}

这是我的前端代码^^

// Changes start
socket.on('join call', (roomId, name) => {
    console.log('New User ', name)

    SessionModel.findById(roomId, (err, session, callback) => {
        if (err) throw err;
        let users = session.usersInCall;
        if (users.length === 10) {
            return;
        }
        let userObject = { 'id' : socket.id, 'name' : name};
        users.push(userObject);
        SessionModel.findOneAndUpdate({_id: roomId}, {usersInCall: users}, {
            new: true
        }, 
        (err) => {
            if (err) throw err
        });

        const usersInThisRoom = users.filter(user => {
            return user['id'] !== socket.id;
        });
        console.log(usersInThisRoom);
        socket.emit("all users", usersInThisRoom);

    })
})

socket.on('sending signal', payload => {
    console.log('Sending Signal',payload['userToSignal'] );

    io.to(payload['userToSignal']).emit('user joined', {
        'signal': payload['signal'],
        'callerId': payload['callerId']
    });
})

socket.on('returning signal', payload => {
    console.log('forwarding signal ',payload['callerId'])
    io.to(payload['callerId']).emit('receiving returned signal', {
        'signal' : payload['signal'],
        'id': socket.id
    });
})

这是我的后端代码^^^

4

2 回答 2

0

尝试将您所有的 socketio 侦听器放入 useEffect

于 2020-10-11T17:14:56.363 回答
0

看起来你已经创建了一个无限循环。后端日志的形式是“转发信号......”应该只打印一次。由于客户端套接字returning signal两次发出事件。添加客户端控制台日志将有助于调试。

于 2021-01-09T13:28:27.680 回答