Vai ai contenuti
function setupVariables(){ getUserMedia = navigator.webkitGetUserMedia.bind(navigator); RTCPeerConnection = webkitRTCPeerConnection; servers =['stun:stun.l.google.com:19302']; } setupVariables(); function connect() { connectButton.disabled = true; disconnectButton.disabled = false; connectToSignalServer(); getUserMedia({audio:false, video:false}, showStream, handleError); } function connectToSignalServer(){ socket = io.connect('http://192.168.5.103:2013'); socket.on('incomingCall', function(message){ createAnswer(message); }); socket.on('receiveAnswer', function(message){ elaborateAnswer(message); }); socket.on('receiveIce', function(message){ receiveIce(message); }); } function showStream(stream){ myVideo.src = window.URL.createObjectURL(stream); localStream = stream; callButton.disabled = false; setupPeerConnection(); } function setupPeerConnection(stream){ peerConnection = new RTCPeerConnection(servers); peerConnection.onicecandidate = gotIceCandidate; peerConnection.addStream(localStream); peerConnection.onaddstream = showRemoteStream; } function callOther() { callButton.disabled = true; peerConnection.createOffer(gotLocalDescriptionOnCall,handleError); } function gotLocalDescriptionOnCall(offer) { peerConnection.setLocalDescription(offer); socket.emit('callRequest', JSON.stringify({'sdp': offer})); }; function createAnswer(message){ var signal = JSON.parse(message); peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() { peerConnection.createAnswer(gotLocalDescriptionOnAnswer); }); } function gotLocalDescriptionOnAnswer(answer) { peerConnection.setLocalDescription(answer); socket.emit('sendAnswer', JSON.stringify({'sdp': answer})); }; function elaborateAnswer(answer){ var signal = JSON.parse(answer); peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)); } function gotIceCandidate(event) { if(event.candidate != null) { socket.emit('sendIce',JSON.stringify({'ice': event.candidate})); } } function receiveIce(message){ var signal = JSON.parse(message); peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)); } function showRemoteStream(event){ otherVideo.src = window.URL.createObjectURL(event.stream); } function toggleSharing = function(){ localStream.getVideoTracks()[0].enabled = !(localStream.getVideoTracks()[0].enabled); localStream.getAudioTracks()[0].enabled = !(localStream.getAudioTracks()[0].enabled); }
Server Per l’implementazione del web-server abbiamo utilizzato Node.Js con la configurazione qui sotto riportata. Salvare la configurazione in un file con nome server.js, e nello stesso folder salvare il file index.html. Lanciare il terminale, portarsi nella directory contenente i files ed eseguire “node server.js”. var static = require('node-static'); var https = require('http'); var file = new(static.Server)(); var app = http.createServer(function (req, res) { file.serve(req, res); }); A questo punto è sufficiente aprire Chrome e richiamare l’indirizzo http://localhost:2003. Utilizzando lo stesso browser aprire un’altra tab e richiamare lo stesso indirizzo.
Torna ai contenuti