index.html (提供者)
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);
});
});
我有另一个对Answererstream.html
执行类似操作的文件。
在setChannelEvents
功能上有通道onmessage的实现。
这在我家里的局域网上完美运行。
我已使用 ngrok 服务将其放到网上以尝试在本地网络之外进行尝试,但无法正常工作。
我正在使用 Google Chrome(更新到最新版本,当前为 35.0.1916.153)。
有什么我想念的吗?有没有我可以尝试的带有 TURN 的 WebRTC 工作示例?
如果需要,我可以添加剩余的代码以及candidate
and的一些输出sdp
。
注意:我启动 ngrok 时修改了套接字上的 IP。