【发布时间】:2015-08-03 09:04:44
【问题描述】:
如何启动一个基本的WebRTC数据通道?
这是我到目前为止所拥有的,但它似乎甚至没有尝试连接。我确定我只是缺少一些基本的东西。
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.msRTCPeerConnection;
var peerConnection = new RTCPeerConnection({
iceServers: [
{url: 'stun:stun1.l.google.com:19302'},
{url: 'stun:stun2.l.google.com:19302'},
{url: 'stun:stun3.l.google.com:19302'},
{url: 'stun:stun4.l.google.com:19302'},
]
});
peerConnection.ondatachannel = function () {
console.log('peerConnection.ondatachannel');
};
peerConnection.onicecandidate = function () {
console.log('peerConnection.onicecandidate');
};
var dataChannel = peerConnection.createDataChannel('myLabel', {
});
dataChannel.onerror = function (error) {
console.log('dataChannel.onerror');
};
dataChannel.onmessage = function (event) {
console.log('dataChannel.onmessage');
};
dataChannel.onopen = function () {
console.log('dataChannel.onopen');
dataChannel.send('Hello World!');
};
dataChannel.onclose = function () {
console.log('dataChannel.onclose');
};
console.log(peerConnection, dataChannel);
【问题讨论】:
-
它没有尝试连接,因为您的代码没有做任何连接。您必须与另一个同行发起提议/答案交换。你用什么发信号?是什么发起了对另一个连接的调用?
-
@xdumaine 是的,我有点不确定,关于如何做到这一点的任何信息?是否可以在没有服务器(即 p2p)的情况下做到这一点?
标签: javascript webrtc