Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Who knows why I can't trigger the onicecandidate event? #721

Open
bbhxwl opened this issue Aug 15, 2024 · 0 comments
Open

Who knows why I can't trigger the onicecandidate event? #721

bbhxwl opened this issue Aug 15, 2024 · 0 comments

Comments

@bbhxwl
Copy link

bbhxwl commented Aug 15, 2024

<script setup lang="ts">
const configuration = {
    iceServers: [
        { urls: 'stun:stun3.l.google.com:19302' } // STUN 服务器配置
    ]
};
let peer1: RTCPeerConnection
let peer2: RTCPeerConnection
onMounted(async ()=>{
    peer1 = new RTCPeerConnection(configuration);
    peer1.onicecandidate = event => {
        console.log(event.candidate)
        if (event.candidate) {
            console.log('发送 ICE candidate: ', event.candidate);
        }
    };
    peer1.onicecandidateerror=event=>{
        console.log("onicecandidateerror",event)
    }
    peer2 = new RTCPeerConnection(configuration);
    peer2.onicecandidate = event => {
        console.log(event.candidate)
        if (event.candidate) {
            console.log('发送 ICE candidate: ', event.candidate);
        }
    };
    peer2.onicecandidateerror=event=>{
        console.log("onicecandidateerror",event)
    }
    const offer=await peer1.createOffer();
    await peer1.setLocalDescription(offer);
    await peer2.setRemoteDescription(offer);
    const answer = await peer2.createAnswer();
    await peer2.setLocalDescription(answer);
    await peer1.setRemoteDescription(answer);
    console.log(peer1.iceConnectionState)
    console.log(peer2.iceConnectionState)
    const dataChannel = peer1.createDataChannel("channel");
    dataChannel.onopen = () => {
        console.log("Data channel is open");
        // 定时发送消息
        setInterval(() => {
            if (dataChannel.readyState === "open") {
                dataChannel.send("Hello from peer1!");
            }
        }, 3000); // 每3秒发送一次消息
    };
    dataChannel.onmessage = event => {
        console.log("Message received by peer1: ", event.data);
    };



    // 在 peer2 监听数据通道
    peer2.ondatachannel = event => {
        const receivedChannel = event.channel;
        receivedChannel.onmessage = (event) => {
            console.log("Message received by peer2: ", event.data);
        };
    };
})

</script>

<template>
    <span></span>
</template>

<style scoped>

</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant