locked
signalR webRTC not function second user RRS feed

  • Question

  • User-1926577155 posted

    Hello People i have problems with WebRTC with SignalR. Must appear the camera of a user on top and the other user below, but so always appears the camera's main user ando the second camera not appers. Basically I want a user 1 establishes a video link with the respective user 2

    In hub i have public static List<UserConnection> usersConnect = new List<UserConnection>();  that save the users.
    And the metod:
    
    public void Send(string message, string userNameTo)
    {
    
    UserConnection connect = usersConnect.FirstOrDefault(x => x.UserName == userNameTo);
    
    
    if (connect!=null)
    {
    
    Clients.Client(connect.ConnectionID).newMessage(message);
    
    }
    
    }
    
    
    
    Initial the users are aditional in the list with override method OnConnected().
    
    
    I have two diferente views but the script code is the same in both, because can be user1 or user2 the first to connect. 
    Is something wrong or missing me something in script? thanks
    
    Script code:
    
    <script type="text/javascript">
    
    
    var _myConnection,
    _myMediaStream;
    
    
    var chat = $.connection.chatHub;
    
    
    $.connection.hub.start().done(function () {
    
    console.log('connected to signal server.');
    //init(); // Start up the app
    
    getUserMedia(
    {
    "video": true,
    "audio": true
    },
    function (stream) {
    
    var localVideo = document.getElementById("localVideo");
    
    // Store off our stream so we can access it later if needed
    _myMediaStream = stream;
    
    // Add the stream to our Video element via adapter.js
    attachMediaStream(localVideo, _myMediaStream);
    
    // Now that we have video, we can make a call
    document.querySelector('#startBtn').removeAttribute('disabled');
    },
    
    // Error callback 
    function (error) {
    // Super nifty error handling
    alert(JSON.stringify(error));
    }
    );
    
    });
    
    
    // Generates a new connection object and ties up the proper callbacks
    function _createConnection() {
    
    //console.log('creating RTCPeerConnection...');
    alert('creating RTCPeerConnection...');
    
    var connection = new RTCPeerConnection(null);
    
    connection.onicecandidate = function (event) {
    if (event.candidate) {
    chat.server.send(JSON.stringify({ "candidate": event.candidate }), "Teste2@gmail.com");
    }
    };
    
    
    
    connection.onaddstream = function (event) {
    
    var remoteVideo = document.getElementById("removeVideo");
    
    attachMediaStream(remoteVideo, event.stream);
    
    // Turn off the call button, since we should be in a call now
    document.querySelector('#startBtn').setAttribute('disabled', 'disabled');
    };
    
    return connection;
    }
    
    
    // -------------------Callback that receives notifications from the SignalR server -> (offer) ou (answer)
    chat.client.newMessage = function (data) {
    
    var message = JSON.parse(data),
    connection = _myConnection || _createConnection(null);
    
    // An SDP message contains connection and media information, and is either an 'offer' or an 'answer'
    if (message.sdp) {
    
    connection.setRemoteDescription(new RTCSessionDescription(message.sdp), function () {
    
    if (connection.remoteDescription.type == 'offer') {
    
    alert("received offer, sending answer...");
    //console.log('received offer, sending answer...');
    
    // Add our stream to the connection to be shared
    connection.addStream(_myMediaStream);
    
    // Create an SDP response
    connection.createAnswer(function (desc) {
    // Which becomes our local session description
    connection.setLocalDescription(desc, function () {
    
    // And send it to the originator, where it will become their RemoteDescription
    chat.server.send(JSON.stringify({ 'sdp': connection.localDescription }), "Teste2@gmail.com");
    });
    
    }, function (error) {
    
    alert("Error creating session description");
    console.log('Error creating session description: ' + error);
    
    });
    }//se for do tipo answer
    else if (connection.remoteDescription.type == 'answer') {
    console.log('got an answer');
    }
    });
    
    } else if (message.candidate) {
    
    //When the peer receives the candidate message, they will construct an RTCIceCandidate object, and add it to the collection of ICE candidates in their RTCPeerConnection.
    console.log('adding ice candidate...');
    
    connection.addIceCandidate(new RTCIceCandidate(message.candidate));
    }
    _myConnection = connection;
    };
    
    
    
    $('#startBtn').click(function () {
    _myConnection = _myConnection || _createConnection(null);
    
    _myConnection.addStream(_myMediaStream); //adding the stream from 'getUserMedia' into the connection object
    
    // Create an offer to send our peer , we need to create and send a WebRTC offer over to the peer we would like to connect with.
    _myConnection.createOffer(function (desc) {
    // Set the generated SDP to be our local session description
    _myConnection.setLocalDescription(desc, function () {
    // And send it to our peer, where it will become their RemoteDescription
    chat.server.send(JSON.stringify({ "sdp": desc }), "Teste2@gmail.com");
    });
    
    }, function (error) {
    
    alert("could not create sdp");
    //console.log('Error creating session description: ' + error);
    });
    
    });
    //}
    
    
    
    </script>


    Tuesday, September 20, 2016 8:55 PM

All replies