我正在尝试使用SignalHub和SimplePeer设置 webrtc 信令服务。但是在执行时出现以下错误:Uncaught (in promise) Error: InvalidStateError: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to set remote answer sdp: Called in wrong state: kStable
.
这是代码:
navigator.mediaDevices.getUserMedia({video: true, audio: false})
.then((stream) => {
const signalhub = require('signalhub')
const hub = signalhub('live-video', [
'http://localhost:8080'
])
const Peer = require('simple-peer')
const peer = new Peer({
initiator: location.hash == '#init',
trickle: false,
stream: stream
})
peer.on('signal', (data) => {
let yourID = JSON.stringify(data)
console.log('your ID: ', yourID)
hub.broadcast('update', yourID)
})
hub.subscribe('update').on('data', (data) => {
let otherID = JSON.parse(data)
console.log('other ID: ', otherID)
peer.signal(otherID)
})
// peer.on('stream', (stream) => {
// const video = document.createElement('video')
// document.body.appendChild(video)
// video.src = window.URL.createObjectURL(stream)
// video.play()
// })
})
.catch((err) => {
document.write(err)
})
我已经按照Kyle Robinson Young的 SignalHub 学习了这两个教程P2P Video Chat with JavaScript / WebRTC & P2P Signaling for WebRTC。