index.html(Offerer)
var socket = io.connect('http://127.0.0.1:80'); //socket.io
socket.emit("player 1");
var iceServers = {
iceServers: [
{"url":"stun:turn1.xirsys.com"},
{"username":"myusername","url":"turn:turn1.xirsys.com:443?transport=udp","credential":"mycredential"},
{"username":"myusername","url":"turn:turn1.xirsys.com:443?transport=tcp","credential":"mycredential"}
]
};
var offererDataChannel, answererDataChannel;
var Offerer = {
createOffer: function () {
var peer = new PeerConnection(iceServers);
var dataChannelOptions = {
reliable: true,
ordered: false
};
offererDataChannel = peer.createDataChannel('channel', dataChannelOptions);
setChannelEvents(offererDataChannel);
peer.onicecandidate = function (event) {
if (event.candidate) {
socket.emit("candidate", event.candidate);
}
};
peer.createOffer(function (sdp) {
peer.setLocalDescription(sdp);
socket.emit("sdp", sdp);
}, function (err) { peer.close(); });
this.peer = peer;
return this;
},
setRemoteDescription: function (sdp) {
this.peer.setRemoteDescription(new SessionDescription(sdp));
},
addIceCandidate: function (candidate) {
this.peer.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
}
};
var Answerer = {
createAnswer: function (offerSDP) {
var peer = new PeerConnection(iceServers);
peer.ondatachannel = function (event) {
answererDataChannel = event.channel;
setChannelEvents(answererDataChannel);
};
peer.onicecandidate = function (event) {
if (event.candidate) {
socket.emit("candidate", event.candidate);
}
};
peer.setRemoteDescription(new SessionDescription(offerSDP));
peer.createAnswer(function (sdp) {
peer.setLocalDescription(sdp);
socket.emit("sdp", sdp);
}, function (err) { peer.close(); });
this.peer = peer;
return this;
},
addIceCandidate: function (candidate) {
this.peer.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
}
};
var peer = null;
socket.on("client is connected", function () {
peer = Offerer.createOffer();
socket.on("candidate", function (candidate) {
peer.addIceCandidate(candidate);
});
socket.on("sdp", function (sdp) {
peer.setRemoteDescription(sdp);
});
});
我有另一个文件stream.html,它为Answerer做了类似的事情.
在setChannelEvents函数上有通道onmessage的实现.
这在我家的局域网上完美运行.
我已经使用ngrok服务将其放在网上,以便在我的本地网络之外进行尝试,并且无法正常工作.
我正在使用谷歌浏览器(更新到最新版本,目前为35.0.1916.153).
有什么东西我不见了吗? WebTTC有TURN的工作示例我可以试试吗?
如果需要,我可以添加剩余的代码加上候选和sdp的一些输出.
注意:当我启动ngrok时,套接字上的IP被修改.
解决方法:
XirSys的家伙在这里. [=
我不太确定你的错误是什么,除了它不起作用.如果错误仅仅是视频没有流动,您应该知道TURN将无法正常工作,因为您已经嵌入了TURN的凭据,而现在已经过期了.使用XirSys时,您必须调用/ getIceServers以获取与您的帐户关联的“新”STUN和TURN服务器集.每次启动呼叫时,都必须发出此POST请求并将结果放入iceServers变量中.
为了快速了解我们的平台,我建议您阅读以下指南:
> Introduction
> Quick start guide
> We also have easy-to-follow guides将其他WebRTC API(包括SimpleWebRTC和EasyRTC)连接到我们的STUN和TURN服务器.
非常感谢您对我们的服务表示出兴趣,如果您有任何问题或意见,请告诉我.