Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

WebRTC datachannel, how to connect between another network?

Discussion in 'Multiplayer' started by kariyama, Jan 21, 2022.

  1. kariyama

    kariyama

    Joined:
    Nov 3, 2020
    Posts:
    1
    I use sample scene "Samples~ > DataChannel" in Unity-Technologies/com.unity.webrtc. This sample, 2 peer in the same pc communicate.But I want to connect between another lan.I use websocket and signaling server.The signaling server relays the received message to other peers as it is.

    I changed the program, to send an offer and receive an answer via websocket, between two UNITY apps.
    However, when I received the answer and executed "remotePeer.SetLocalDescription (ref desc)", I got an error that the fingerprint dose not match.
    When and where was the fingerprint printed in the first place?
    Is it when created a peer with "localPeer = new RTCPeerConnection (ref configuration) "?

    The processing flow is as follows.

    1.Create local and remote peers with app1 and app2, respectively.

    2.Create an offer in app1 and set the local DESC of the local peer, set the remote DESC of the remote peer.
    Send the offer to app2 via a signaling server via websocket.

    3.App2 sets the received offer to the local DESC of the remote peer, and sets it to the remote DESC of the local peer. Create an answer and set the local DESC of the local peer, set the remote DESC of the remote peer, and send the answer to app1.

    4.App1 sets the received answer to the remote peer's local DESC (fingerprint not match here) and sets it to the local peer's remote DESC.



    the process that causes an error when receiving an answer

    Code (CSharp):
    1.    
    2.     //1.make peer and offer
    3.     IEnumerator Call()
    4.     {
    5.         callButton.interactable = false;
    6.         Debug.Log("GetSelectedSdpSemantics");
    7.         var configuration = GetSelectedSdpSemantics();
    8.         localPeer = new RTCPeerConnection(ref configuration);
    9.         Debug.Log("Created local peer connection object pc1");
    10.         localPeer.OnIceCandidate = pc1OnIceCandidate;
    11.         localPeer.OnIceConnectionChange = pc1OnIceConnectionChange;
    12.  
    13.         remotePeer = new RTCPeerConnection(ref configuration);
    14.         Debug.Log("Created remote peer connection object pc2");
    15.         remotePeer.OnIceCandidate = pc2OnIceCandidate;
    16.         remotePeer.OnIceConnectionChange = pc2OnIceConnectionChange;
    17.         remotePeer.OnDataChannel = onDataChannel;
    18.  
    19.         RTCDataChannelInit conf = new RTCDataChannelInit();
    20.         dataChannel = localPeer.CreateDataChannel("data", conf);
    21.         dataChannel.OnOpen = onDataChannelOpen;
    22.  
    23.         Debug.Log("pc1 createOffer start");
    24.         var op1 = localPeer.CreateOffer();
    25.         yield return op1;
    26.  
    27.         if (!op1.IsError)
    28.         {
    29.             yield return StartCoroutine(OnCreateOfferSuccess(op1.Desc));
    30.         }
    31.         else
    32.         {
    33.             OnCreateSessionDescriptionError(op1.Error);
    34.         }
    35.     }
    36.  
    37.     //2.offer send to app2
    38.    IEnumerator OnCreateOfferSuccess(RTCSessionDescription op1offerdesc)
    39.     {
    40.         Debug.Log($"Offer from pc1\n{op1offerdesc.sdp}");
    41.         Debug.Log("pc1 setLocalDescription start");
    42.         var op2 = localPeer.SetLocalDescription(ref op1offerdesc);
    43.         yield return op2;
    44.  
    45.         if (!op2.IsError)
    46.         {
    47.             OnSetLocalSuccess(localPeer);
    48.         }
    49.         else
    50.         {
    51.             var error = op2.Error;
    52.             OnSetSessionDescriptionError(ref error);
    53.         }
    54.  
    55.         Debug.Log("pc2 setRemoteDescription start");
    56.         var op3 = remotePeer.SetRemoteDescription(ref op1offerdesc);
    57.         yield return op3;
    58.         if (!op3.IsError)
    59.         {
    60.             OnSetRemoteSuccess(remotePeer);
    61.         }
    62.         else
    63.         {
    64.             var error = op3.Error;
    65.             OnSetSessionDescriptionError(ref error);
    66.         }
    67.         int i = (int)op1offerdesc.type;
    68.         ws.Send(i.ToString() + "," + op1offerdesc.sdp);
    69.     }
    70.  
    71.     //3.In app2,recieve offer. Then make answer and send answer to app1.
    72.     //4.In app1,recieve answer.
    73.     IEnumerator getRemoteMessage(RTCSessionDescription desc) {
    74.  
    75.         if (localPeer != null) {
    76.  
    77.             Debug.Log("get answer!");
    78.  
    79.             Debug.Log($"Answer from pc2\n{desc.sdp}");
    80.             Debug.Log("remotePeer setLocalDescription start");
    81.             var op5 = remotePeer.SetLocalDescription(ref desc);
    82.             yield return op5;
    83.  
    84.             if (!op5.IsError) {
    85.                 OnSetLocalSuccess(remotePeer);
    86.             } else {
    87.                 Debug.Log(op5.Error.message);//                 ←error happen!
    88.                 var error = op5.Error;
    89.                 OnSetSessionDescriptionError(ref error);
    90.             }
    91.  
    92.             Debug.Log("localPeer setRemoteDescription start");
    93.  
    94.             var op6 = localPeer.SetRemoteDescription(ref desc);
    95.             if (!op6.IsError) {
    96.                 OnSetRemoteSuccess(localPeer);
    97.             } else {
    98.                 var error = op6.Error;
    99.                 OnSetSessionDescriptionError(ref error);
    100.             }
    101.  
    102.  
    103.         } else {
    104.             Debug.Log("get offer!");
    105.  
    106.             //localPeer and remotePeer instantiate
    107.             Call2();
    108.  
    109.             //受け取ったおフォーのSDPからpc2ローカルを作成
    110.             var op2 = remotePeer.SetLocalDescription(ref desc);
    111.             if (!op2.IsError) {
    112.                 OnSetLocalSuccess(remotePeer);
    113.             } else {
    114.                 var error = op2.Error;
    115.                 OnSetSessionDescriptionError(ref error);
    116.             }
    117.  
    118.             //pc2ローカル=pc1のリモート
    119.             var op3 = localPeer.SetRemoteDescription(ref desc);
    120.             if (!op3.IsError) {
    121.                 OnSetRemoteSuccess(localPeer);
    122.             } else {
    123.                 var error = op3.Error;
    124.                 OnSetSessionDescriptionError(ref error);
    125.             }
    126.  
    127.             //アンサーを作成
    128.             var op4 = localPeer.CreateAnswer();
    129.             yield return op4;
    130.             if (!op4.IsError) {
    131.                 yield return OnCreateAnswerSuccess(op4.Desc);
    132.             } else {
    133.                 OnCreateSessionDescriptionError(op4.Error);
    134.             }
    135.         }
    136.     }
    137.  
    138.     //localPeer and remotePeer instantiate
    139.     void Call2() {
    140.         callButton.interactable = false;
    141.         Debug.Log("GetSelectedSdpSemantics");
    142.         var configuration = GetSelectedSdpSemantics();
    143.         localPeer = new RTCPeerConnection(ref configuration);
    144.         Debug.Log("Created local peer connection object pc1");
    145.         localPeer.OnIceCandidate = pc1OnIceCandidate;
    146.         localPeer.OnIceConnectionChange = pc1OnIceConnectionChange;
    147.  
    148.         remotePeer = new RTCPeerConnection(ref configuration);
    149.         Debug.Log("Created remote peer connection object pc2");
    150.         remotePeer.OnIceCandidate = pc2OnIceCandidate;
    151.         remotePeer.OnIceConnectionChange = pc2OnIceConnectionChange;
    152.         remotePeer.OnDataChannel = onDataChannel;
    153.  
    154.         RTCDataChannelInit conf = new RTCDataChannelInit();
    155.         dataChannel = remotePeer.CreateDataChannel("data", conf);
    156.         dataChannel.OnOpen = onDataChannelOpen;
    157.     }
    158.  
    159.     //send answer
    160.     IEnumerator OnCreateAnswerSuccess(RTCSessionDescription op4AnswerDesc)
    161.     {
    162.  
    163.         Debug.Log($"Answer from pc1:\n{op4AnswerDesc.sdp}");
    164.         Debug.Log("pc1 setLocalDescription start");
    165.         var op5 = localPeer.SetLocalDescription(ref op4AnswerDesc);
    166.         yield return op5;
    167.  
    168.         if (!op5.IsError)
    169.         {
    170.             OnSetLocalSuccess(localPeer);
    171.         } else
    172.         {
    173.             var error = op5.Error;
    174.             OnSetSessionDescriptionError(ref error);
    175.         }
    176.  
    177.         //pc1のローカル=pc2のリモート
    178.         Debug.Log("pc2 setRemoteDescription start");
    179.  
    180.         var op6 = remotePeer.SetRemoteDescription(ref op4AnswerDesc);
    181.         yield return op6;
    182.         if (!op6.IsError)
    183.         {
    184.             OnSetRemoteSuccess(remotePeer);
    185.         }
    186.         else
    187.         {
    188.             var error = op6.Error;
    189.             OnSetSessionDescriptionError(ref error);
    190.         }
    191.  
    192.         //リモートにアンサーのdescを投げる @@@
    193.         ws.Send(((int)(op4AnswerDesc.type)).ToString() + "," + op4AnswerDesc.sdp);
    194.  
    195.     }
     
    ronaldoUnited07 likes this.
  2. shrirammvasudevan

    shrirammvasudevan

    Joined:
    Mar 31, 2020
    Posts:
    9
    Were you able to fix this and if so how? Am also having issues with connecting two remote users. Thanks!
     
  3. ronaldoUnited07

    ronaldoUnited07

    Joined:
    Mar 2, 2023
    Posts:
    4