2015-05-10 3 views
0

Я делаю приложение для видеочата WebRTC, и он работал до того, как я начал добавлять или вычитать больше кода, и в процессе я удалил или изменил порядок таким образом, что теперь я получаю эту ошибку. К сожалению, у меня нет резервного кода, и он так сильно поглотил мое время.ReferenceError: номер не определен, RTCpeerconnection не работает. Клиенты не подключаются

Вам понадобятся пакеты socket.io и node-static, установленные на вашем сервере узла.

Хорошо, мне удалось решить проблему с ошибкой, но теперь клиенты не соединяются друг с другом, кажется, что два клиента не могут обмениваться сообщениями через сервер.

Мой server.js код ниже

var static = require('node-static'); 
var http = require('http'); 
var file = new(static.Server)(); 
var app = http.createServer(function (req, res) { 
    file.serve(req, res); 

}).listen(8080, '127.0.0.1'); 

var io = require('socket.io').listen(app); 
     io.sockets.on('connection', function (socket){ 
     socket.emit('emit(): client ' + socket.id + ' joined room ' + room); 
     socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room); 


    function log(){ 
     var array = [">>> Message from server: "]; 
     for (var i = 0; i < arguments.length; i++) { 
     array.push(arguments[i]); 
     } 
     socket.emit('log', array); 
    } 

    socket.on('message', function (message) { 
     log('Got message: ', message); 
    // For a real app, should be room only (not broadcast) 
     socket.broadcast.emit('message', message); 
    }); 

    socket.on('create or join', function (room) { 
     var numClients = io.sockets.clients(room).length; 

     log('Room ' + room + ' has ' + numClients + ' client(s)'); 
     log('Request to create or join room', room); 

     if (numClients == 0){ 
      socket.join(room); 
      socket.emit('created', room); 
     } else if (numClients == 1) { 
      io.sockets.in(room).emit('join', room); 
      socket.join(room); 
      socket.emit('joined', room); 
     } else { // max two clients 
      socket.emit('full', room); 
     } 

    }); 

}); 

мой application.js файл следующим образом

'use strict'; 

var isChannelReady; 
var isInitiator = false; 
var isStarted = false; 
var localStream; 
var pc; 
var remoteStream; 


var pc_config = webrtcDetectedBrowser === 'firefox' ? 
    {'iceServers':[{'url':'stun:23.21.150.121'}]} : // number IP 
    {'iceServers': [{'url': 'stun:stun.l.google.com:19302'}]}; 

var pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': true}]}; 

// Set up audio and video regardless of what devices are present. 
var sdpConstraints = {'mandatory': { 
    'OfferToReceiveAudio':true, 
    'OfferToReceiveVideo':true }}; 


var room = location.pathname.substring(1); 
if (room === '') { 
room = window.prompt('Enter room name:'); 
room = ''; 
} 

var socket = io.connect(); 



if (room !== '') { 
    console.log('Create or join room', room); 
    socket.emit('create or join', room); 
} 

socket.on('created', function (room){ 
    console.log('Created room ' + room); 
    isInitiator = true; 
}); 

socket.on('full', function (room){ 
    console.log('Room ' + room + ' is full'); 
}); 

socket.on('join', function (room){ 
    console.log('Another peer made a request to join room ' + room); 
    console.log('This peer is the initiator of room ' + room + '!'); 
    isChannelReady = true; 
}); 

socket.on('joined', function (room){ 
    console.log('This peer has joined room ' + room); 
    isChannelReady = true; 
}); 

socket.on('log', function (array){ 
    console.log.apply(console, array); 
}); 

//////////////////////////////////////////////// 

function sendMessage(message){ 
    console.log('Client sending message: ', message); 
    // if (typeof message === 'object') { 
    // message = JSON.stringify(message); 
    // } 
    socket.emit('message', message); 
} 

socket.on('message', function (message){ 
    console.log('Client received message:', message); 
    if (message === 'got user media') { 
    maybeStart(); 
    } else if (message.type === 'offer') { 
    if (!isInitiator && !isStarted) { 
     maybeStart(); 
    } 
    pc.setRemoteDescription(new RTCSessionDescription(message)); 
    doAnswer(); 
    } else if (message.type === 'answer' && isStarted) { 
    pc.setRemoteDescription(new RTCSessionDescription(message)); 
    } else if (message.type === 'candidate' && isStarted) { 
    var candidate = new RTCIceCandidate({ 
     sdpMLineIndex: message.label, 
     candidate: message.candidate 
    }); 
    pc.addIceCandidate(candidate); 
    } else if (message === 'bye' && isStarted) { 
    handleRemoteHangup(); 
    } 
}); 

//////////////////////////////////////////////////// 



function handleUserMedia(stream) { 
    console.log('Adding local stream.'); 
    localVideo.src = window.URL.createObjectURL(stream); 
    localStream = stream; 
    sendMessage('got user media'); 
    if (isInitiator) { 
    maybeStart(); 
    } 
} 

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

var constraints = {video: true, audio:true}; 
getUserMedia(constraints, handleUserMedia, handleUserMediaError); 

console.log('Getting user media with constraints', constraints); 



function maybeStart() { 
    if (!isStarted && typeof localStream != 'undefined' && isChannelReady) { 
    createPeerConnection(); 
    pc.addStream(localStream); 
    isStarted = true; 
    console.log('isInitiator', isInitiator); 
    if (isInitiator) { 
     doCall(); 
    } 
    } 
} 

window.onbeforeunload = function(e){ 
    sendMessage('bye'); 
} 

///////////////////////////////////////////////////////// 

function createPeerConnection() { 
    try { 
    pc = new RTCPeerConnection(null); 
    pc.onicecandidate = handleIceCandidate; 
    pc.onaddstream = handleRemoteStreamAdded; 
    pc.onremovestream = handleRemoteStreamRemoved; 
    console.log('Created RTCPeerConnnection'); 
    } catch (e) { 
    console.log('Failed to create PeerConnection, exception: ' + e.message); 
    alert('Cannot create RTCPeerConnection object.'); 
     return; 
    } 
} 

function handleIceCandidate(event) { 
    console.log('handleIceCandidate event: ', event); 
    if (event.candidate) { 
    sendMessage({ 
     type: 'candidate', 
     label: event.candidate.sdpMLineIndex, 
     id: event.candidate.sdpMid, 
     candidate: event.candidate.candidate}); 
    } else { 
    console.log('End of candidates.'); 
    } 
} 

function handleRemoteStreamAdded(event) { 
    console.log('Remote stream added.'); 
    remoteVideo.src = window.URL.createObjectURL(event.stream); 
    remoteStream = event.stream; 
} 




function handleCreateOfferError(event){ 
    console.log('createOffer() error: ', e); 
} 

function doCall() { 
    console.log('Sending offer to peer'); 
    pc.createOffer(setLocalAndSendMessage, handleCreateOfferError); 
} 

function doAnswer() { 
    console.log('Sending answer to peer.'); 
    pc.createAnswer(setLocalAndSendMessage, null, sdpConstraints); 
} 

function setLocalAndSendMessage(sessionDescription) { 
    // Set Opus as the preferred codec in SDP if Opus is present. 
    sessionDescription.sdp = preferOpus(sessionDescription.sdp); 
    pc.setLocalDescription(sessionDescription); 
    console.log('setLocalAndSendMessage sending message' , sessionDescription); 
    sendMessage(sessionDescription); 
} 



function handleRemoteStreamAdded(event) { 
    console.log('Remote stream added.'); 
    remoteVideo.src = window.URL.createObjectURL(event.stream); 
    remoteStream = event.stream; 
} 

function handleRemoteStreamRemoved(event) { 
    console.log('Remote stream removed. Event: ', event); 
} 

function hangup() { 
    console.log('Hanging up.'); 
    stop(); 
    sendMessage('bye'); 
} 

function handleRemoteHangup() { 
// console.log('Session terminated.'); 
    // stop(); 
    // isInitiator = false; 
} 

function stop() { 
    isStarted = false; 
    // isAudioMuted = false; 
    // isVideoMuted = false; 
    pc.close(); 
    pc = null; 
} 

/////////////////////////////////////////// 

// Set Opus as the default audio codec if it's present. 
function preferOpus(sdp) { 
    var sdpLines = sdp.split('\r\n'); 
    var mLineIndex = null; 
    // Search for m line. 
    for (var i = 0; i < sdpLines.length; i++) { 
     if (sdpLines[i].search('m=audio') !== -1) { 
     mLineIndex = i; 
     break; 
     } 
    } 
    if (mLineIndex === null) { 
    return sdp; 
    } 

    // If Opus is available, set it as the default in m line. 
    for (i = 0; i < sdpLines.length; i++) { 
    if (sdpLines[i].search('opus/48000') !== -1) { 
     var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i); 
     if (opusPayload) { 
     sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], opusPayload); 
     } 
     break; 
    } 
    } 

    // Remove CN in m line and sdp. 
    sdpLines = removeCN(sdpLines, mLineIndex); 

    sdp = sdpLines.join('\r\n'); 
    return sdp; 
} 

function extractSdp(sdpLine, pattern) { 
    var result = sdpLine.match(pattern); 
    return result && result.length === 2 ? result[1] : null; 
} 

// Set the selected codec to the first in m line. 
function setDefaultCodec(mLine, payload) { 
    var elements = mLine.split(' '); 
    var newLine = []; 
    var index = 0; 
    for (var i = 0; i < elements.length; i++) { 
    if (index === 3) { // Format of media starts from the fourth. 
     newLine[index++] = payload; // Put target payload to the first. 
    } 
    if (elements[i] !== payload) { 
     newLine[index++] = elements[i]; 
    } 
    } 
    return newLine.join(' '); 
} 

// Strip CN from sdp before CN constraints is ready. 
function removeCN(sdpLines, mLineIndex) { 
    var mLineElements = sdpLines[mLineIndex].split(' '); 
    // Scan from end for the convenience of removing an item. 
    for (var i = sdpLines.length-1; i >= 0; i--) { 
    var payload = extractSdp(sdpLines[i], /a=rtpmap:(\d+) CN\/\d+/i); 
    if (payload) { 
     var cnPos = mLineElements.indexOf(payload); 
     if (cnPos !== -1) { 
     // Remove CN payload from m line. 
     mLineElements.splice(cnPos, 1); 
     } 
     // Remove CN line in sdp 
     sdpLines.splice(i, 1); 
    } 
    } 

    sdpLines[mLineIndex] = mLineElements.join(' '); 
    return sdpLines; 
} 

index.html

<!DOCTYPE html> 
<html> 
<head> 

<meta name='keywords' content='WebRTC, HTML5, JavaScript' /> 
<meta name='description' content='WebRTC Reference App' /> 
<meta name='viewport' content='width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1'> 

<base target='_blank'> 

<title>WebRTC client</title> 

<link rel='stylesheet' href='css/style.css' /> 

</head> 

<body id='body'> 

<p style=" font-size:24px" align="center">WebRTC Video Share</p> 

<div id='container'> 

<div> 
    <video id='localVideo' autoplay muted></video> 
    <video id='remoteVideo' autoplay></video> 
</div> 

</div> 

<script src='/socket.io/socket.io.js'></script> 
<script src='js/lib/adapter.js'></script> 
<script src='js/main.js'></script> 

</body> 
</html> 
+0

Это клиентская или серверная ошибка? Какой номер строки? – joews

+0

кажется, что после того, как строка создания комнаты 32 не выполнена. он должен отправить сообщение на сервер для создания или присоединения к комнате. Мне удалось исправить ошибку Reference, но теперь у меня есть новая проблема. – Rookie9

ответ

1

В коде server.js:

var io = require('socket.io').listen(app); 
    io.sockets.on('connection', function (socket){ 
    socket.emit('emit(): client ' + socket.id + ' joined room ' + room); 

В конце вы ссылаетесь на переменную room, но вы еще не создали ее.

+0

кажется, что после того, как строка создания комнаты 32 не выполнена. он должен отправить сообщение на сервер для создания или присоединения к комнате. Мне удалось исправить ошибку после того, что вы предложили ранее, можете ли вы, пожалуйста, помочь дальше ?. – Rookie9

Смежные вопросы