diff --git a/public/main.js b/public/main.js index e195688..74d17fe 100644 --- a/public/main.js +++ b/public/main.js @@ -1,6 +1,8 @@ // client-side js, loaded by index.html // run by the browser each time the page is loaded +let Peer = window.Peer; + let messagesEl = document.querySelector('.messages'); let peerIdEl = document.querySelector('#connect-to-peer'); let videoEl = document.querySelector('.remote-video'); @@ -15,6 +17,7 @@ let renderVideo = (stream) => { videoEl.srcObject = stream; }; +// Register with the peer server let peer = new Peer({ host: '/', path: '/peerjs/myapp' @@ -25,6 +28,8 @@ peer.on('open', (id) => { peer.on('error', (error) => { console.error(error); }); + +// Handle incoming data connection peer.on('connection', (conn) => { logMessage('incoming peer connection!'); conn.on('data', (data) => { @@ -34,6 +39,8 @@ peer.on('connection', (conn) => { conn.send('hello!'); }); }); + +// Handle incoming voice/video connection peer.on('call', (call) => { navigator.mediaDevices.getUserMedia({video: true, audio: true}) .then((stream) => { @@ -45,6 +52,7 @@ peer.on('call', (call) => { }); }); +// Initiate outgoing connection let connectToPeer = () => { let peerId = peerIdEl.value; logMessage(`Connecting to ${peerId}...`); diff --git a/server.js b/server.js index e25151a..46183f8 100644 --- a/server.js +++ b/server.js @@ -3,8 +3,10 @@ // we've started you off with Express (https://expressjs.com/) // but feel free to use whatever libraries or frameworks you'd like through `package.json`. -const { ExpressPeerServer } = require("peer"); const express = require("express"); + +const { ExpressPeerServer } = require("peer"); + const app = express(); // make all the files in 'public' available