使用WebRTC在两个对等端之间创建和使用数据通道

5

我正在尝试使用WebRTC设置点对点文件共享系统。我能够在每一端打开数据通道,但我无法从一个用户发送消息到另一个用户。而且,如果一个用户关闭通道,则onclose事件仅针对此用户触发。

使用WebRTC设置和使用数据通道的正确方法是什么?

您能告诉我我的代码有什么问题或缺失吗?

//create RTC peer objet.
var RTCPeerConnection = webkitRTCPeerConnection;
var RTCIceCandidate = window.RTCIceCandidate;
var RTCSessionDescription = window.RTCSessionDescription;

var iceServers = {
    iceServers: [{
        url: 'stun:stun.l.google.com:19302'
    }]
};
var p2p_connection = new RTCPeerConnection({
      iceServers: [
        { 'url': (IS_CHROME ? 'stun:stun.l.google.com:19302' : 'stun:23.21.150.121') }
  ]
});

// send offer (only executes in one browser)
function initiateConnection() {
    p2p_connection.createOffer(function (description) {
        p2p_connection.setLocalDescription(description);
        server_socket.emit('p2p request', description,my_username);
    });
};

// receive offer and send answer
server_socket.on('p2p request', function(description,sender){ 
    console.log('received p2p request');

    p2p_connection.setRemoteDescription(new RTCSessionDescription(description));

    p2p_connection.createAnswer(function (description) {
        p2p_connection.setLocalDescription(description);
        server_socket.emit('p2p reply', description,sender);
    });
});

// receive answer
server_socket.on('p2p reply', function(description,sender){
    console.log('received p2p reply');
    p2p_connection.setRemoteDescription(new RTCSessionDescription(description));
});

// ICE candidates
p2p_connection.onicecandidate = onicecandidate; // sent event listener

// locally generated
function onicecandidate(event) {
    if (!p2p_connection || !event || !event.candidate) return;
    var candidate = event.candidate;
    server_socket.emit('add candidate',candidate,my_username);
}

// sent by other peer
server_socket.on('add candidate', function(candidate,my_username){

    p2p_connection.addIceCandidate(new RTCIceCandidate({
            sdpMLineIndex: candidate.sdpMLineIndex,
            candidate: candidate.candidate
    }));
});

// data channel 
var dataChannel = p2p_connection.createDataChannel('label');

dataChannel.onmessage = function (event) {
    var data = event.data;
    console.log("I got data channel message: ", data);
};

dataChannel.onopen = function (event) {
    console.log("Data channel ready");
    dataChannel.send("Hello World!");
};
dataChannel.onclose = function (event) {
    console.log("Data channel closed.");
};
dataChannel.onerror = function (event) {
    console.log("Data channel error!");
}

更新:

在这里找到了解决方案:http://www.html5rocks.com/en/tutorials/webrtc/basics/

p2p_connection.ondatachannel = function (event) {
    receiveChannel = event.channel;
    receiveChannel.onmessage = function(event){
    console.log(event.data);
    };
};

1
很高兴你找到了答案:)。继续发布你自己的答案,你可以接受它以供后人参考。 - Benjamin Trent
2个回答

4
你可以考虑使用 simple-peer 库,以避免将来处理这些复杂性。WebRTC API 调用很混乱,有时候难以正确排序。 simple-peer 支持视频/音频流、数据通道(文本和二进制数据),甚至可以将数据通道用作 node.js 风格的双工流。它还支持高级选项,如禁用涓涓细流 ICE 候选者(因此每个客户端只需发送一个 offer/answer 消息,而不是重复的 ice 候选消息)。它是开放式的,并且可以与任何后端一起使用。 https://github.com/feross/simple-peer


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接