0

我是 Javascript 的新编码。我正在我的 iPhone 和浏览器之间创建一个 WebRTC 连接。连接有效,但我的代码只发送一个候选人,我不知道我是否做错了什么。我将不胜感激任何评论或支持。

谢谢

const createPeerConnection = (signaling) => {
            const peerConnection = new RTCPeerConnection({
                iceServers: [],
            });

            const offerOptions = {
                offerToReceiveVideo: true, offerToReceiveAudio: true
            };

            peerConnection.createOffer(offerOptions);
    
            createAndSendOffer(signaling, peerConnection);

            peerConnection.onicecandidate = (iceEvent) => {
                if (iceEvent && iceEvent.candidate) {
                    signaling.send(JSON.stringify({
                        type: MESSAGE_TYPE.IceCandidate,
                        payload: iceEvent.candidate,
                    }));
                }
            };
    
            peerConnection.onconnectionstatechange = (state ) => {
                console.log(peerConnection.connectionState);
            };
    
            return peerConnection;
        };

const createAndSendOffer = async (signaling, peerConnection) => {
        const offer = await peerConnection.createOffer();
        await peerConnection.setLocalDescription(offer);
        signaling.send(JSON.stringify({ type: MESSAGE_TYPE.SessionDescription, payload: offer }));
    };

                
4

0 回答 0