我正在 React Native Android 中实现 webrtc 实时语音聊天。我使用 coturn 构建了一个 Turn 服务器。但是,如果您认为coturn的配置不正确,并且购买并应用了ExpressTURN的turn服务器,则webrtc将只能在本地环境中工作。它唯一一次在 wifi 上作为 LTE 工作是在我使用 coturn 的时候。通信通过 https 进行,但证书使用根证书并且不是由权威机构颁发的。证书尚未应用到turn服务器。
我无法找出原因。
如果有人找出原因并解决了这个症状,请帮助我。
wifi(private) -> wifi(private) = success
lte -> lte = success
wifi(private) -> lte = success
lte -> wifi (private) = failure
wifi(hotspot) -> lte = success
lte -> wifi (hotspot) = failure
async initialize() {
try {
await this.initializeData()
this.peerConnection = new RTCPeerConnection({
iceServers: [
// { urls: 'stun:stun.l.google.com:19302' },
{
urls: [
"turn:??",
"turn:??",
"turn:??",
"turn:??",
"turn:??",
],
username: '??',
credential: '??',
}
]
})
this.localStream = await mediaDevices.getUserMedia({ audio: true, video: false })
this.localStream.getTracks().forEach(track => this.peerConnection.addTrack(track, this.localStream))
// 피어 이벤트
this.peerConnection.addEventListener('track', this.onTrackEvent)
this.peerConnection.addEventListener('icecandidate', this.onIcecandidateEvent)
this.peerConnection.addEventListener('connectionstatechange', (event) => {
console.log(`connectionState`, this.peerConnection?.connectionState)
})
this.peerConnection.addEventListener('iceconnectionstatechange', (event) => {
console.log(`iceConnectionState`, this.peerConnection?.iceConnectionState)
})
this.peerConnection.addEventListener('signalingstatechange', () => {
console.log(`signalingState`, this.peerConnection?.signalingState)
})
} catch (error) {
console.error(error)
}
}
onTrackEvent = (event) => {
try {
this.remoteStream = event.streams[0]
} catch (error) {
console.error(error)
}
}
onIcecandidateEvent = (event) => {
try {
if (event.candidate) {
socketManager.socket?.emit('WebRTC_ICEcandidate', { target: id, candidate: event.candidate })
}
} catch (error) {
console.error(error)
}
}
onIcecandidateEvent = (event) => {
try {
if (event.candidate) {
socketManager.socket?.emit('WebRTC_ICEcandidate', { target: id, candidate: event.candidate })
}
} catch (error) {
console.error(error)
}
}
async startOffer() {
try {
const offer = await this.peerConnection?.createOffer()
await this.peerConnection?.setLocalDescription(offer)
socket?.emit('WebRTC_startOffer', { target: id, offer: offer })
} catch (error) {
console.error(error)
}
}
async answerOffer({ offer }) {
try {
await this.peerConnection?.setRemoteDescription(offer)
const answer = await this.peerConnection?.createAnswer()
await this.peerConnection?.setLocalDescription(answer)
socket?.emit('WebRTC_answerOffer', { target: id, answer: answer })
} catch (error) {
console.error(error)
}
}
async setRemoteOffer({ answer }) {
try {
await this.peerConnection?.setRemoteDescription(answer)
} catch (error) {
console.error(error)
}
}
async setRemoteIceCandidate({ iceCandidate }) {
try {
if (this.peerConnection) {
this.peerConnection?.addIceCandidate(new RTCIceCandidate(iceCandidate))
}
} catch (error) {
console.error(error)
}
}