diff --git a/public/main.js b/public/main.js index e50ec90..9ade8a7 100644 --- a/public/main.js +++ b/public/main.js @@ -28,16 +28,20 @@ var app = new Vue({ this.connections.push(conn); console.log('[Out] connected', conn); conn.on('data', (data) => { - this.logMessage(`received: ${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); - console.log('[Out] Connection closed', 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}.`); }); @@ -51,17 +55,14 @@ var app = new Vue({ }, hangUp: function() { this.calls.forEach(c => { - this.disconnectCall(c); + c.close(); + }); + this.connections.forEach(c => { + c.close(); }); - }, - disconnectCall: function(call) { - if (!call) return; - console.log('CALL', call); - call.close(); }, listenForPeerEvents: function () { this.peer.on('open', (id) => { - this.logMessage('My peer ID is: ' + id); localStorage.setItem('yaswvc-peerId', id); }); this.peer.on('error', (error) => { @@ -70,21 +71,22 @@ var app = new Vue({ // Handle incoming data connection this.peer.on('connection', (conn) => { - this.logMessage('incoming peer connection!'); this.connections.push(conn); console.log('[INC] incoming peer connection!', conn); conn.on('data', (data) => { - this.logMessage(`received: ${data}`); + this.logMessage(`${conn.peer}: ${data}`); }); conn.on('open', () => { conn.send('hello!'); }); conn.on('close', () => { this.connections = this.connections.filter(c => c.connectionId === conn.connectionId); - this.logMessage('Connection closed.'); + this.calls = this.calls.filter(c => c.peer !== conn.peer); + this.logMessage(`${conn.peer} closed connection.`); console.log('[Inc] closed conenction', conn) }); conn.on('error', () => { + this.connections = this.connections.filter(c => c.connectionId === conn.connectionId); console.warn(`[Inc] Connection error ${conn.connectionId} with ${conn.peer}.`); }); });