Transferring JSON between browsers with WebRTC

前端 未结 7 1573
一向
一向 2020-12-30 04:32

I was excited by the prospect of WebRTC when I heard about it initially. It sounded like websockets but without a server. Unfortunately, all of the tutorials I have been a

相关标签:
7条回答
  • 2020-12-30 05:08

    This is an old question, but because I started to learn webRTC, I will attempt to answer it.

    First of all, some misconception:

    It sounded like websockets but without a server

    There is no way ANY data can be transferred between WebRTC peers before some information (Media session management, Nodes’ network configuration/multimedia capabilities) has been properly exchanged and negotiated. To do this you need a server and signalling (which is not a part of webRTC: you can implement it the way you want).

    When the signaling is done, you need to create RTCPeerConnection with something like this:

    if (navigator.webkitGetUserMedia) {
       RTCPeerConnection = webkitRTCPeerConnection;
    } else if(navigator.mozGetUserMedia){
       RTCPeerConnection = mozRTCPeerConnection;
       RTCSessionDescription = mozRTCSessionDescription;
       RTCIceCandidate = mozRTCIceCandidate;
    }
    

    and then:

    var connection = new RTCPeerConnection(servers);
    

    After this you can add your data channel to this PeerConnection:

    var dataChannel = connection.createDataChannel("channelName",{ reliable: true });
    

    When this is done you can just call sendChannel.send('Any data you want'); and this will send any data you want.

    If anything, I found this book really helpful. It leaves a lot of unanswered questions, but for the first start it is good.

    0 讨论(0)
提交回复
热议问题