From 27e91cc10fa0ae14284a15f1de2aca6715b25b59 Mon Sep 17 00:00:00 2001 From: "Glitch (peerjs-video)" Date: Sat, 12 Sep 2020 22:12:31 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=90=F0=9F=8F=85=20Checkpoint=20./publi?= =?UTF-8?q?c/main.js:5975361/7152=20./views/index.ejs:5975361/569?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/main.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ views/index.ejs | 3 +++ 2 files changed, 63 insertions(+) diff --git a/public/main.js b/public/main.js index 320be53..2e5a8af 100644 --- a/public/main.js +++ b/public/main.js @@ -66,6 +66,44 @@ var app = new Vue({ console.log('Started call', call); this.handleCall(call); }, + connectToPeer2: async function(peerId) { + if (peerId === this.peer.id) { + this.logMessage(`You played yourself! Can't connect to yourself.`); + return; + }; + this.logMessage(`Connecting to ${peerId}...`); + + let conn = this.peer.connect(peerId); + this.connections.push(conn); + console.log('[Out] connected', conn); + conn.on('data', (data) => { + this.logMessage(`${conn.peer}: ${data}`); + }); + conn.on('open', () => { + conn.send('hi!'); + }); + conn.on('close', () => { + this.connections = this.connections.filter(c => c.connectionId === conn.connectionId); + this.calls = this.calls.filter(c => c.peer !== conn.peer); + this.logMessage(`${conn.peer} closed connection.`); + console.log(`[Out] Connection closed with ${conn.peer}.`); + }); + conn.on('error', () => { + this.connections = this.connections.filter(c => c.connectionId === conn.connectionId); + this.calls = this.calls.filter(c => c.peer !== conn.peer); + console.warn(`[Out] Connection error ${conn.connectionId} with ${conn.peer}.`); + }); + + const audioStream = await this.getAudioStream(); + let audioCall = this.peer.call(peerId, audioStream); + console.log('Started audioCall', audioCall); + this.calls.push(audioCall); + const videoStream = await this.getVideoStream(); + let videoCall = this.peer.call(peerId, videoStream); + console.log('Started videoCall', videoCall); + this.calls.push(videoCall); + // this.handleCall(call); + }, hangUp: function() { this.calls.forEach(c => { console.log('hangUp - closing', c); @@ -227,6 +265,28 @@ var app = new Vue({ // Refresh button list in case labels have become available return navigator.mediaDevices.enumerateDevices(); }, + getAudioStream: async function getAudioStream() { + const audioSource = document.querySelector('select#audioSource').value; + // const videoSource = document.querySelector('select#videoSource').value; + const constraints = { + audio: {deviceId: audioSource ? {exact: audioSource} : undefined}, + // video: {deviceId: undefined} + }; + const audioStream = await navigator.mediaDevices.getUserMedia(constraints).catch(this.handleError); + console.log('GOT AUDIO STREAM', audioStream); + return audioStream; + }, + getVideoStream: async function getVideoStream() { + // const audioSource = document.querySelector('select#audioSource').value; + const videoSource = document.querySelector('select#videoSource').value; + const constraints = { + // audio: {deviceId: audioSource ? {exact: audioSource} : undefined}, + video: {deviceId: videoSource ? {exact: videoSource} : undefined} + }; + const videoStream = await navigator.mediaDevices.getUserMedia(constraints).catch(this.handleError); + console.log('GOT VIDEO STREAM', videoStream); + return videoStream; + }, shareScreen: async function shareScreen() { // if (!displayMediaStream) { const displayMediaStream = await navigator.mediaDevices.getDisplayMedia(); diff --git a/views/index.ejs b/views/index.ejs index cfb8934..50a79ce 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -95,6 +95,9 @@ +