html - peerConnection.addIceCandidate 给出错误 : invalid string

标签 html node.js websocket socket.io webrtc

我正在尝试实现纯语音 WebRTC 应用程序。我在 Chrome Version 29.0.1547.0 dev 上运行它。我的应用使用 Socket.IO 作为信号机制。

peerConnection.addIceCandidate() 给我这个错误:Uncaught SyntaxError: An invalid or illegal string was specified.

另外,peerConnection.setRemoteDescription(); 给我这个错误:Uncaught TypeMismatchError: The type of an object was incompatible with the expected type of parameter associated to the object.

这是我的代码:

服务器(在 CoffeeScript 中)

app = require("express")()
server = require("http").createServer(app).listen(3000)
io = require("socket.io").listen(server)

app.get "/", (req, res) -> res.sendfile("index.html")
app.get "/client.js", (req, res) -> res.sendfile("client.js")

io.sockets.on "connection", (socket) ->
    socket.on "message", (data) ->
        socket.broadcast.emit "message", data

客户端(在 JavaScript 中)

var socket = io.connect("http://localhost:3000");

var pc = new webkitRTCPeerConnection({
    "iceServers": [{"url": "stun:stun.l.google.com:19302"}]
});


navigator.getUserMedia = navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia;

navigator.getUserMedia({audio: true}, function (stream) {
    pc.addStream(stream);
}, function (error) { console.log(error); });


pc.onicecandidate = function (event) {
    if (!event || !event.candidate) return;
    socket.emit("message", {
        type: "iceCandidate",
        "candidate": event.candidate
    });
};


pc.onaddstream = function(event) {
    var audioElem = document.createElement("audio");
    audioElem.src = webkitURL.createObjectURL(event.stream);
    audioElem.autoplay = true;
    document.appendChild(audioElem);
    console.log("Got Remote Stream");
};


socket.on("message", function(data) {
    if (data.type === "iceCandidate") {
        console.log(data.candidate);

        candidate = new RTCIceCandidate(data.candidate);
        console.log(candidate);

        pc.addIceCandidate(candidate);

    } else if (data.type === "offer") {
        pc.setRemoteDescription(data.description);
        pc.createAnswer(function(description) {
            pc.setLocalDescription(description);
            socket.emit("message", {type: "answer", description: description});
        });
    } else if (data.type === "answer") {
        pc.setRemoteDescription(data.description);
    }
});


function offer() {
    pc.createOffer( function (description) {
        pc.setLocalDescription(description);
        socket.emit("message", {type: "offer", "description": description});
    });
};

HTML 仅包含一个调用 offer() 的按钮。

我可以确认 ICECandidatesSessionDescriptions 正在从一个客户端成功传输到另一个客户端。

我做错了什么?我应该如何修复这些错误和任何其他错误,以便我可以将音频从一个客户端传输到另一个客户端?

PS:如果您知道记录 WebRTC API 的良好来源(W3C 文档除外),请告诉我!

谢谢!

最佳答案

对于那个错误,重点是,只有在成功设置远程描述后,才必须添加 ICE 候选人。

请注意,在(由 Offerer)创建 Offer 之后,会立即生成 ice candidates。因此,如果应答者以某种方式收到这些候选人,在设置远程描述之前(理论上它会在候选人之前到达),您会收到错误。

提供者也是如此。它必须在添加任何候选冰之前设置远程描述。

我看到在您的 javascript 代码中您不保证在添加 ice candidates 之前设置了远程描述。

首先,您可以在 pc.addIceCandidate(candidate); 之前检查是否设置了 pc 的 remoteDescription。如果您看到它为 null(或未定义),您可以在本地存储收到的 ice candidates 以在设置 remoteDescription 后添加它们(或等待 offerer 在适当的时间发送它们。)

关于html - peerConnection.addIceCandidate 给出错误 : invalid string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17346616/

相关文章:

html - 为什么当宽度为100vw时,x轴上会出现滚动?

ruby-on-rails - Node/NPM 依赖于 Ruby on Rails 引擎 gem Assets 管道

python - 使用 nlp 从句子中挑选主语 + 形容词对

node.js - Socket.IO 身份验证逻辑似乎有效,但无论如何都允许未经身份验证的套接字

Django Channels Worker 没有响应 websocket.connect

javascript - Websockets blob 解码的性能导致内存问题

c# - 如何使用 mshtml 将复选框设置为 "checked"?

jquery - 在自定义 Accordion 导航上管理打开和关闭的菜单

javascript - HTML5 音频标签无法为 Chrome 和 Safari 中第一个未聚焦的选项卡播放声音?

json - 用 Cheerio 抓取,文字不可见