我正在使用库 simple-peer 尝试 webrtc。我正在关注在 Opera 浏览器上链接的本教程。发送一小串“你好”时,我遇到了这个错误:
Uncaught DOMException: Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open' at Peer.send (http://127.0.0.1:9966/bundle.js:7011:19) at HTMLButtonElement.<anonymous> (http://127.0.0.1:9966/bundle.js:22:14) send @ index.js:241 (anonymous) @ index.js:21
这是我的 index.js:
var Peer = require('simple-peer')
if (Peer.WEBRTC_SUPPORT) {
console.log("Support");
peer = new Peer({
initiator: location.hash === '#1',
trickle:true
})
peer.on('signal', function (data) {
document.getElementById('yourId').value = JSON.stringify(data)
})
document.getElementById("connect").addEventListener('click', function () {
var otherId = JSON.parse(document.getElementById('otherId').value)
peer.signal(otherId)
})
peer.on('connect', function () {
console.log("CONNECTED");
})
document.getElementById("send").addEventListener('click', function () {
var message = document.getElementById("message_to_send").value
peer.write(message)
})
peer.on("data", function (data) {
console.log(data)
document.getElementById("messages").textContent += data + "\n"
})
} else {
console.log("No Support, Sorry");
}
更改
peer.send(data)
为
peer.write(data)
确实会给出任何错误,但也不会将数据发送到其他浏览器
我究竟做错了什么?提前致谢