gpt4 book ai didi

javascript - 如何启动一个基本的 WebRTC 数据通道?

转载 作者:数据小太阳 更新时间:2023-10-29 04:34:43 26 4
gpt4 key购买 nike

如何启动一个基本的 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);

最佳答案

WebRTC 假定您有办法向任何您希望联系的人发送信号(向其发送报价字符串,并从中接收答复字符串)。如果没有一些服务器,您将如何做到这一点?

为了说明,这里有一些代码可以完成除此之外的所有操作(适用于 Firefox 和 Chrome 45):

var config = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }]};
var dc, pc = new RTCPeerConnection(config);
pc.ondatachannel = e => {
dc = e.channel;
dc.onopen = e => (log("Chat!"), chat.select());
dc.onmessage = e => log(e.data);
}

function createOffer() {
button.disabled = true;
pc.ondatachannel({ channel: pc.createDataChannel("chat") });
pc.createOffer().then(d => pc.setLocalDescription(d)).catch(failed);
pc.onicecandidate = e => {
if (e.candidate) return;
offer.value = pc.localDescription.sdp;
offer.select();
answer.placeholder = "Paste answer here";
};
};

offer.onkeypress = e => {
if (e.keyCode != 13 || pc.signalingState != "stable") return;
button.disabled = offer.disabled = true;
var obj = { type:"offer", sdp:offer.value };
pc.setRemoteDescription(new RTCSessionDescription(obj))
.then(() => pc.createAnswer()).then(d => pc.setLocalDescription(d))
.catch(failed);
pc.onicecandidate = e => {
if (e.candidate) return;
answer.focus();
answer.value = pc.localDescription.sdp;
answer.select();
};
};

answer.onkeypress = e => {
if (e.keyCode != 13 || pc.signalingState != "have-local-offer") return;
answer.disabled = true;
var obj = { type:"answer", sdp:answer.value };
pc.setRemoteDescription(new RTCSessionDescription(obj)).catch(failed);
};

chat.onkeypress = e => {
if (e.keyCode != 13) return;
dc.send(chat.value);
log(chat.value);
chat.value = "";
};

var log = msg => div.innerHTML += "<p>" + msg + "</p>";
var failed = e => log(e + ", line " + e.lineNumber);
<script src="https://rawgit.com/webrtc/adapter/master/adapter.js"></script>
<button id="button" onclick="createOffer()">Offer:</button>
<textarea id="offer" placeholder="Paste offer here"></textarea><br>
Answer: <textarea id="answer"></textarea><br><div id="div"></div>
Chat: <input id="chat"></input><br>

在第二个选项卡中打开此页面,您可以从一个选项卡聊天到另一个选项卡(或世界各地的另一台机器)。糟糕的是,您必须自己在那里获得报价:

  • (仅)按选项卡 A 中的报价 按钮并等待 1-20 秒,直到您看到报价文本,
  • 将报价文本从选项卡 A 复制粘贴到选项卡 B,然后按 Enter
  • 将选项卡 B 中显示的答案文本复制并粘贴到选项卡 A,然后按 Enter

您现在应该可以在没有服务器的情况下在选项卡之间聊天。

如您所见,这是一种低于标准的体验,这就是为什么您需要一些基本的 websocket 服务器来在 A 和 B 之间传递报价/答案(如果您想快速连接,还需要滴流冰候选),开始做事。建立连接后,您可以为此使用数据通道,但需要做一些额外的工作。

关于javascript - 如何启动一个基本的 WebRTC 数据通道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31718574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com