我正在尝试实现仅语音的 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 文档除外),请告诉我!
谢谢!