使用Websockets的网络错误12030

时间:2016-12-13 07:41:54

标签: node.js websocket webrtc

您好我正在将Wowza Media Server与Temasys插件集成(用于支持crossbrowser),并且代码在Chrome上运行良好,但在IE11 +上它给我网络错误。请参阅附图和代码了解详情。

const GO_BUTTON_START = "Publish";
const GO_BUTTON_STOP = "Stop";
var video = document.querySelector('video');


var peerConnection = null;
var peerConnectionConfig = {'iceServers': []};
var localStream = null;
var wsURL = "wss://localhost.streamlock.net/webrtc-session.json";
var wsConnection = null;
var streamInfo = {applicationName:"webrtc", streamName:"myStream", sessionId:"[empty]"};
var userData = {param1:"value1"};
var videoBitrate = 360;
var audioBitrate = 64;
var newAPI = false;
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
//var constraints = {
  //audio: false,
  //video: true
//};

function successCallback(stream) {
  window.stream = stream; // make stream available to browser console
    localStream=stream;
    console.log(localStream);
  video = attachMediaStream(video, stream); 
}

function errorCallback(error) {
  console.log('navigator.getUserMedia error: ', error);
}

function pageReady()
{

    $("#buttonGo").attr('value', GO_BUTTON_START);



    var constraints =
    {
        video: true,
        audio: false,
    };
//alert('here');
navigator.getUserMedia(constraints, successCallback, errorCallback);

    console.log("newAPI: "+newAPI);

}
function wsConnect(url)
{
    wsConnection = new WebSocket(url);
    wsConnection.binaryType = 'arraybuffer';

    wsConnection.onopen = function()
    {
        console.log("wsConnection.onopen");

        peerConnection = new RTCPeerConnection(peerConnectionConfig);
        peerConnection.onicecandidate = gotIceCandidate;

        if (newAPI)
        {
            var localTracks = localStream.getTracks();
            for(localTrack in localTracks)
            {
                console.log("video stream"+localStream);
                peerConnection.addTrack(localTracks[localTrack], localStream);
            }
        }
        else
        {
        console.log("video stream"+localStream);
            peerConnection.addStream(localStream);
        }

        peerConnection.createOffer(gotDescription, errorHandler);
    }

    wsConnection.onmessage = function(evt)
    {
        console.log("wsConnection.onmessage: "+evt.data);

        var msgJSON = JSON.parse(evt.data);

        var msgStatus = Number(msgJSON['status']);
        var msgCommand = msgJSON['command'];

        if (msgStatus != 200)
        {
            $("#sdpDataTag").html(msgJSON['statusDescription']);
            stopPublisher();
        }
        else
        {
            $("#sdpDataTag").html("");

            var sdpData = msgJSON['sdp'];
            if (sdpData !== undefined)
            {
                console.log('sdp: '+msgJSON['sdp']);

                peerConnection.setRemoteDescription(new RTCSessionDescription(sdpData), function() {
                    //peerConnection.createAnswer(gotDescription, errorHandler);
                }, errorHandler);
            }

            var iceCandidates = msgJSON['iceCandidates'];
            if (iceCandidates !== undefined)
            {
                for(var index in iceCandidates)
                {
                    console.log('iceCandidates: '+iceCandidates[index]);

                    peerConnection.addIceCandidate(new RTCIceCandidate(iceCandidates[index]));
                }
            }
        }

        if (wsConnection != null)
            wsConnection.close();
        wsConnection = null;

}
    wsConnection.onclose = function(error)
    {
        console.log("wsConnection.onclose"+error);
    }

    wsConnection.onerror = function(evt)
    {
        console.log("wsConnection.onerror: "+JSON.stringify(evt));


        $("#sdpDataTag").html('WebSocket connection failed: '+wsURL);
        stopPublisher();
    }

}

function startPublisher()
{
    wsURL = $('#sdpURL').val();
    streamInfo.applicationName = $('#applicationName').val();
    streamInfo.streamName = $('#streamName').val();
    videoBitrate = $('#videoBitrate').val();
    audioBitrate = $('#audioBitrate').val();

    $.cookie("webrtcPublishWSURL", wsURL, { expires: 365 });
    $.cookie("webrtcPublishApplicationName", streamInfo.applicationName, { expires: 365 });
    $.cookie("webrtcPublishStreamName", streamInfo.streamName, { expires: 365 });
    $.cookie("webrtcPublishVideoBitrate", videoBitrate, { expires: 365 });
    $.cookie("webrtcPublishAudioBitrate", audioBitrate, { expires: 365 });

    console.log("startPublisher: wsURL:"+wsURL+" streamInfo:"+JSON.stringify(streamInfo));

    wsConnect(wsURL);

    $("#buttonGo").attr('value', GO_BUTTON_STOP);
}
function stopPublisher()
{
    if (peerConnection != null)
        peerConnection.close();
    peerConnection = null;

    if (wsConnection != null)
        wsConnection.close();
    wsConnection = null;

    $("#buttonGo").attr('value', GO_BUTTON_START);

    console.log("stopPublisher");
}


function btn_start()
{
    alert('button clicked');
    if (peerConnection == null)
        startPublisher();
    else
        stopPublisher();
}

function gotIceCandidate(event)
{
    if(event.candidate != null)
    {
        //console.log('gotIceCandidate: '+JSON.stringify({'ice': event.candidate}));
    }
}

function gotDescription(description)
{   
    var enhanceData = new Object();

    if (audioBitrate !== undefined) 
        enhanceData.audioBitrate = Number(audioBitrate);
    if (videoBitrate !== undefined) 
        enhanceData.videoBitrate = Number(videoBitrate);

    description.sdp = enhanceSDP(description.sdp, enhanceData);

    console.log('gotDescription: '+JSON.stringify({'sdp': description}));

    peerConnection.setLocalDescription(description, function () {

        wsConnection.send('{"direction":"publish", "command":"sendOffer", "streamInfo":'+JSON.stringify(streamInfo)+', "sdp":'+JSON.stringify(description)+', "userData":'+JSON.stringify(userData)+'}');
console.log("stream info"+JSON.stringify(streamInfo));
console.log("stream description"+JSON.stringify(description));
console.log("stream userData"+JSON.stringify(userData));

    }, function() {console.log('set description error')});
}

function enhanceSDP(sdpStr, enhanceData)
{
    var sdpLines = sdpStr.split(/\r\n/);
    var sdpSection = 'header';
    var hitMID = false;
    var sdpStrRet = '';

    for(var sdpIndex in sdpLines)
    {
        var sdpLine = sdpLines[sdpIndex];

        if (sdpLine.length <= 0)
            continue;

        sdpStrRet += sdpLine+'\r\n';

        if (sdpLine.indexOf("m=audio") === 0)
        {
            sdpSection = 'audio';
            hitMID = false;
        }
        else if (sdpLine.indexOf("m=video") === 0)
        {
            sdpSection = 'video';
            hitMID = false;
        }

        if (sdpLine.indexOf("a=mid:") === 0)
        {
            if (!hitMID)
            {
                if ('audio'.localeCompare(sdpSection) == 0)
                {
                    if (enhanceData.audioBitrate !== undefined)
                    {
                        sdpStrRet += 'b=AS:' + enhanceData.audioBitrate + '\r\n';
                        sdpStrRet += 'b=TIAS:' + (enhanceData.audioBitrate*1024) + '\r\n';
                    }
                }
                else if ('video'.localeCompare(sdpSection) == 0)
                {
                    if (enhanceData.videoBitrate !== undefined)
                    {
                        sdpStrRet += 'b=AS:' + enhanceData.videoBitrate + '\r\n';
                        sdpStrRet += 'b=TIAS:' + (enhanceData.videoBitrate*1024) + '\r\n';
                    }
                }
                hitMID = true;
            }
        }
    }

    return sdpStrRet;
}
function errorHandler(error)
{
    console.log(error);
}

Click here to see image

0 个答案:

没有答案