【发布时间】:2013-06-25 03:49:09
【问题描述】:
我正在尝试实现仅语音的 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.
这是我的代码:
服务器(在 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() 的按钮。
我可以确认ICECandidates 和SessionDescriptions 正在成功地从一个客户端转移到另一个客户端。
我做错了什么?我应该如何修复这些错误以及任何其他错误,以便将音频从一个客户端传输到另一个客户端?
PS:如果您知道记录 WebRTC API 的良好来源(W3C 文档除外),请告诉我!
谢谢!
【问题讨论】:
-
您能否粘贴您传递给 RTCIceCandidate 构造函数的 data.candidate 示例?
-
我无法回答您的具体问题,但我发现WebRTC-Series of html5rocks 是关于该主题的一个很好的资源。
-
我无法解决这个问题,即使在远程描述设置之前缓存远程候选冰。 RTCIceCandidate {sdpMLineIndex: 1, sdpMid: "", Candidate: "a=candidate:924013166 1 tcp 1509957375 192.168.57.1 0 typ host generation 0 ↵"} app.js:13895 Uncaught SyntaxError:指定了无效或非法的字符串。
标签: html node.js websocket socket.io webrtc