How to create data channel in WebRTC peer connection?

后端 未结 3 624
你的背包
你的背包 2021-01-31 06:24

I\'m trying to learn how to create an RTCPeerConnection so that I can use the DataChannel API. Here\'s what I have tried from what I understood:

<
3条回答
  •  误落风尘
    2021-01-31 06:52

    I've posted a gist that shows setting up a data connection, compatible with both Chrome and Firefox.

    The main difference is that where in FF you have to wait until the connection is set up, in Chrome it's just the opposite: it seems you need to create the data connection before any offers are sent back/forth:

    var pc1 = new RTCPeerConnection(cfg, con);
    if (!pc1.connectDataConnection) setupDC1();    // Chrome...Firefox defers per other answer
    

    The other difference is that Chrome passes an event object to .ondatachannel whereas FF passes just a raw channel:

    pc2.ondatachannel = function (e) {
        var datachannel = e.channel || e;
    

    Note that you currently need Chrome Nightly started with --enable-data-channels for it to work as well.

提交回复
热议问题