Commit 30b37f67 authored by Moritz Langenstein's avatar Moritz Langenstein
Browse files

(ml5717) (alh1717) (ztw17) Remove duplicate (possibly stale) old connections on reconnect

Removal up old unused stale connections still necessary (i.e. disconnected peer changed their ID) -> might need heartbeat / dropped messages analysis
parent 7ba21cb4
...@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", { ...@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _wildemitter = require('wildemitter'); var _wildemitter = require('wildemitter');
...@@ -104,13 +106,17 @@ var LioWebRTC = function (_WildEmitter) { ...@@ -104,13 +106,17 @@ var LioWebRTC = function (_WildEmitter) {
var peers = self.webrtc.getPeers(message.from, message.roomType); var peers = self.webrtc.getPeers(message.from, message.roomType);
var totalPeers = self.webrtc.getPeers().length; var totalPeers = self.webrtc.getPeers().length;
var peer = void 0; var peer = void 0;
if (message.type === 'offer') { if (message.type === 'offer') {
if (peers.length) { if (peers.length) {
peers.forEach(function (p) { peers.forEach(function (p) {
if (p.sid === message.sid) peer = p; if (p.sid === message.sid) peer = p;
}); });
// if (!peer) peer = peers[0]; // fallback for old protocol versions peers.forEach(function (p) {
if (p !== peer) {
p.end(false);
totalPeers--;
}
});
} }
if (_this.config.dataOnly && _this.config.constraints.maxPeers > 0 && totalPeers >= _this.config.constraints.maxPeers) { if (_this.config.dataOnly && _this.config.constraints.maxPeers > 0 && totalPeers >= _this.config.constraints.maxPeers) {
return; return;
...@@ -490,7 +496,6 @@ var LioWebRTC = function (_WildEmitter) { ...@@ -490,7 +496,6 @@ var LioWebRTC = function (_WildEmitter) {
var peer = void 0; var peer = void 0;
_this5.roomCount = Object.keys(roomDescription.clients).length; _this5.roomCount = Object.keys(roomDescription.clients).length;
// console.log(roomDescription);
_this5.id = roomDescription.you; _this5.id = roomDescription.you;
(0, _PeerOptimizer.addNode)(_this5.id); (0, _PeerOptimizer.addNode)(_this5.id);
_this5.unconnectivePeers[_this5.id] = true; _this5.unconnectivePeers[_this5.id] = true;
...@@ -507,27 +512,38 @@ var LioWebRTC = function (_WildEmitter) { ...@@ -507,27 +512,38 @@ var LioWebRTC = function (_WildEmitter) {
client = roomDescription.clients[id]; client = roomDescription.clients[id];
for (type in client) { for (type in client) {
if (type !== 'turnservers' && client[type]) { if (type !== 'turnservers' && client[type]) {
console.log('Received peer info about', id, 'on room join'); var _ret = function () {
var peerCount = _this5.webrtc.getPeers().length; console.log('Received peer info about', id, 'on room join');
if (_this5.config.dataOnly && _this5.config.constraints.maxPeers > 0 && (peerCount >= _this5.config.constraints.minPeers || peerCount >= _this5.config.constraints.maxPeers)) { var peerCount = _this5.webrtc.getPeers().length;
return; var peers = self.webrtc.getPeers(id, type);
} peers.forEach(function (p) {
peer = self.webrtc.createPeer({ p.end(false);
id: id, peerCount--;
type: type, });
enableDataChannels: self.config.enableDataChannels && type !== 'screen', if (_this5.config.dataOnly && _this5.config.constraints.maxPeers > 0 && (peerCount >= _this5.config.constraints.minPeers || peerCount >= _this5.config.constraints.maxPeers)) {
receiveMedia: { return {
offerToReceiveAudio: type !== 'screen' && !_this5.config.dataOnly && _this5.config.receiveMedia.offerToReceiveAudio ? 1 : 0, v: void 0
offerToReceiveVideo: !_this5.config.dataOnly && self.config.receiveMedia.offerToReceiveVideo ? 1 : 0 };
}, }
iceServers: self.webrtc.config.peerConnectionConfig.iceServers.concat(client.turnservers) peer = self.webrtc.createPeer({
}); id: id,
if (_this5.config.dataOnly && _this5.config.constraints.maxPeers > 0) { type: type,
_this5.sendPing(peer, peer.id, true); enableDataChannels: self.config.enableDataChannels && type !== 'screen',
} else { receiveMedia: {
peer.start(); offerToReceiveAudio: type !== 'screen' && !_this5.config.dataOnly && _this5.config.receiveMedia.offerToReceiveAudio ? 1 : 0,
_this5.emit('createdPeer', peer); offerToReceiveVideo: !_this5.config.dataOnly && self.config.receiveMedia.offerToReceiveVideo ? 1 : 0
} },
iceServers: self.webrtc.config.peerConnectionConfig.iceServers.concat(client.turnservers)
});
if (_this5.config.dataOnly && _this5.config.constraints.maxPeers > 0) {
_this5.sendPing(peer, peer.id, true);
} else {
peer.start();
_this5.emit('createdPeer', peer);
}
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
} }
} }
} }
...@@ -607,31 +623,44 @@ var LioWebRTC = function (_WildEmitter) { ...@@ -607,31 +623,44 @@ var LioWebRTC = function (_WildEmitter) {
}, { }, {
key: 'connectToPeer', key: 'connectToPeer',
value: function connectToPeer(peerId, client) { value: function connectToPeer(peerId, client) {
var _this7 = this;
var type = void 0; var type = void 0;
var peer = void 0; var peer = void 0;
for (type in client) { for (type in client) {
if (type !== 'turnservers' && client[type]) { if (type !== 'turnservers' && client[type]) {
console.log('Connecting to random peer', peerId); var _ret2 = function () {
var peerCount = this.webrtc.getPeers().length; console.log('Connecting to random peer', peerId);
if (this.config.constraints.maxPeers > 0 && peerCount >= this.config.constraints.maxPeers) { var peerCount = _this7.webrtc.getPeers().length;
return; var peers = self.webrtc.getPeers(peerId, type);
} peers.forEach(function (p) {
peer = this.webrtc.createPeer({ p.end(false);
id: peerId, peerCount--;
type: type, });
enableDataChannels: this.config.enableDataChannels && type !== 'screen', if (_this7.config.constraints.maxPeers > 0 && peerCount >= _this7.config.constraints.maxPeers) {
receiveMedia: { return {
offerToReceiveAudio: type !== 'screen' && !this.config.dataOnly && this.config.receiveMedia.offerToReceiveAudio ? 1 : 0, v: void 0
offerToReceiveVideo: !this.config.dataOnly && this.config.receiveMedia.offerToReceiveVideo ? 1 : 0 };
}, }
iceServers: self.webrtc.config.peerConnectionConfig.iceServers.concat(client.turnservers) peer = _this7.webrtc.createPeer({
}); id: peerId,
if (this.config.dataOnly && this.config.constraints.maxPeers > 0) { type: type,
this.sendPing(peer, peerId, true); enableDataChannels: _this7.config.enableDataChannels && type !== 'screen',
} else { receiveMedia: {
peer.start(); offerToReceiveAudio: type !== 'screen' && !_this7.config.dataOnly && _this7.config.receiveMedia.offerToReceiveAudio ? 1 : 0,
this.emit('createdPeer', peer); offerToReceiveVideo: !_this7.config.dataOnly && _this7.config.receiveMedia.offerToReceiveVideo ? 1 : 0
} },
iceServers: self.webrtc.config.peerConnectionConfig.iceServers.concat(client.turnservers)
});
if (_this7.config.dataOnly && _this7.config.constraints.maxPeers > 0) {
_this7.sendPing(peer, peerId, true);
} else {
peer.start();
_this7.emit('createdPeer', peer);
}
}();
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
} }
} }
} }
......
...@@ -60,15 +60,19 @@ class LioWebRTC extends WildEmitter { ...@@ -60,15 +60,19 @@ class LioWebRTC extends WildEmitter {
connection.on('message', (message) => { connection.on('message', (message) => {
const peers = self.webrtc.getPeers(message.from, message.roomType); const peers = self.webrtc.getPeers(message.from, message.roomType);
const totalPeers = self.webrtc.getPeers().length; let totalPeers = self.webrtc.getPeers().length;
let peer; let peer;
if (message.type === 'offer') { if (message.type === 'offer') {
if (peers.length) { if (peers.length) {
peers.forEach((p) => { peers.forEach((p) => {
if (p.sid === message.sid) peer = p; if (p.sid === message.sid) peer = p;
}); });
// if (!peer) peer = peers[0]; // fallback for old protocol versions peers.forEach((p) => {
if (p !== peer) {
p.end(false);
totalPeers--;
}
});
} }
if (this.config.dataOnly && this.config.constraints.maxPeers > 0 && totalPeers >= this.config.constraints.maxPeers) { if (this.config.dataOnly && this.config.constraints.maxPeers > 0 && totalPeers >= this.config.constraints.maxPeers) {
return; return;
...@@ -404,7 +408,6 @@ class LioWebRTC extends WildEmitter { ...@@ -404,7 +408,6 @@ class LioWebRTC extends WildEmitter {
let peer; let peer;
this.roomCount = Object.keys(roomDescription.clients).length; this.roomCount = Object.keys(roomDescription.clients).length;
// console.log(roomDescription);
this.id = roomDescription.you; this.id = roomDescription.you;
addNode(this.id); addNode(this.id);
this.unconnectivePeers[this.id] = true; this.unconnectivePeers[this.id] = true;
...@@ -413,7 +416,12 @@ class LioWebRTC extends WildEmitter { ...@@ -413,7 +416,12 @@ class LioWebRTC extends WildEmitter {
for (type in client) { for (type in client) {
if (type !== 'turnservers' && client[type]) { if (type !== 'turnservers' && client[type]) {
console.log('Received peer info about', id, 'on room join') console.log('Received peer info about', id, 'on room join')
const peerCount = this.webrtc.getPeers().length; let peerCount = this.webrtc.getPeers().length;
const peers = self.webrtc.getPeers(id, type);
peers.forEach((p) => {
p.end(false);
peerCount--;
});
if (this.config.dataOnly && this.config.constraints.maxPeers > 0 && (peerCount >= this.config.constraints.minPeers || peerCount >= this.config.constraints.maxPeers)) { if (this.config.dataOnly && this.config.constraints.maxPeers > 0 && (peerCount >= this.config.constraints.minPeers || peerCount >= this.config.constraints.maxPeers)) {
return; return;
} }
...@@ -493,7 +501,12 @@ class LioWebRTC extends WildEmitter { ...@@ -493,7 +501,12 @@ class LioWebRTC extends WildEmitter {
for (type in client) { for (type in client) {
if (type !== 'turnservers' && client[type]) { if (type !== 'turnservers' && client[type]) {
console.log('Connecting to random peer', peerId) console.log('Connecting to random peer', peerId)
const peerCount = this.webrtc.getPeers().length; let peerCount = this.webrtc.getPeers().length;
const peers = self.webrtc.getPeers(peerId, type);
peers.forEach((p) => {
p.end(false);
peerCount--;
});
if (this.config.constraints.maxPeers > 0 && peerCount >= this.config.constraints.maxPeers) { if (this.config.constraints.maxPeers > 0 && peerCount >= this.config.constraints.maxPeers) {
return; return;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment