peerConnection.addIceCandidate错误:无效的string

我正在尝试实现仅限语音的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 the parameter associated to the object.

这是我的代码:

SERVER (在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()的button。

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

我究竟做错了什么? 我应该如何解决这些和其他错误,以便我可以将audio从一个客户端传输到另一个客户端?

PS:如果你知道loggingWebRTC API的好源(除了W3C文档),请告诉我!

谢谢!

对于这个错误,ICE候选人必须在成功设置远程描述后才能添加。

请注意,在创buildOffer(by Offerer)之后,立即生成候选冰块。 所以,如果回答者收到这些候选人,在设置远程描述之前(理论上这将在候选人之前到达),那么就会出错。

提供者也是如此。 在添加候选人之前,必须设置远程描述。

我看到在你的javascript代码中,你不能保证在添加候选人之前设置远程描述。

首先你可以在pc.addIceCandidate(candidate);之前检查pc.addIceCandidate(candidate); 如果PC的remoteDescription设置。 如果你看到它是空的(或未定义的),你可以在本地存储接收到的候选冰,在设置remoteDescription之后添加它们(或等待发送者在适当的时候发送它们)。