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", {
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 _wildemitter = require('wildemitter');
......@@ -104,13 +106,17 @@ var LioWebRTC = function (_WildEmitter) {
var peers = self.webrtc.getPeers(message.from, message.roomType);
var totalPeers = self.webrtc.getPeers().length;
var peer = void 0;
if (message.type === 'offer') {
if (peers.length) {
peers.forEach(function (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) {
return;
......@@ -490,7 +496,6 @@ var LioWebRTC = function (_WildEmitter) {
var peer = void 0;
_this5.roomCount = Object.keys(roomDescription.clients).length;
// console.log(roomDescription);
_this5.id = roomDescription.you;
(0, _PeerOptimizer.addNode)(_this5.id);
_this5.unconnectivePeers[_this5.id] = true;
......@@ -507,27 +512,38 @@ var LioWebRTC = function (_WildEmitter) {
client = roomDescription.clients[id];
for (type in client) {
if (type !== 'turnservers' && client[type]) {
console.log('Received peer info about', id, 'on room join');
var peerCount = _this5.webrtc.getPeers().length;
if (_this5.config.dataOnly && _this5.config.constraints.maxPeers > 0 && (peerCount >= _this5.config.constraints.minPeers || peerCount >= _this5.config.constraints.maxPeers)) {
return;
}
peer = self.webrtc.createPeer({
id: id,
type: type,
enableDataChannels: self.config.enableDataChannels && type !== 'screen',
receiveMedia: {
offerToReceiveAudio: type !== 'screen' && !_this5.config.dataOnly && _this5.config.receiveMedia.offerToReceiveAudio ? 1 : 0,
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);
}
var _ret = function () {
console.log('Received peer info about', id, 'on room join');
var peerCount = _this5.webrtc.getPeers().length;
var peers = self.webrtc.getPeers(id, type);
peers.forEach(function (p) {
p.end(false);
peerCount--;
});
if (_this5.config.dataOnly && _this5.config.constraints.maxPeers > 0 && (peerCount >= _this5.config.constraints.minPeers || peerCount >= _this5.config.constraints.maxPeers)) {
return {
v: void 0
};
}
peer = self.webrtc.createPeer({
id: id,
type: type,
enableDataChannels: self.config.enableDataChannels && type !== 'screen',
receiveMedia: {
offerToReceiveAudio: type !== 'screen' && !_this5.config.dataOnly && _this5.config.receiveMedia.offerToReceiveAudio ? 1 : 0,
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) {
}, {
key: 'connectToPeer',
value: function connectToPeer(peerId, client) {
var _this7 = this;
var type = void 0;
var peer = void 0;
for (type in client) {
if (type !== 'turnservers' && client[type]) {
console.log('Connecting to random peer', peerId);
var peerCount = this.webrtc.getPeers().length;
if (this.config.constraints.maxPeers > 0 && peerCount >= this.config.constraints.maxPeers) {
return;
}
peer = this.webrtc.createPeer({
id: peerId,
type: type,
enableDataChannels: this.config.enableDataChannels && type !== 'screen',
receiveMedia: {
offerToReceiveAudio: type !== 'screen' && !this.config.dataOnly && this.config.receiveMedia.offerToReceiveAudio ? 1 : 0,
offerToReceiveVideo: !this.config.dataOnly && this.config.receiveMedia.offerToReceiveVideo ? 1 : 0
},
iceServers: self.webrtc.config.peerConnectionConfig.iceServers.concat(client.turnservers)
});
if (this.config.dataOnly && this.config.constraints.maxPeers > 0) {
this.sendPing(peer, peerId, true);
} else {
peer.start();
this.emit('createdPeer', peer);
}
var _ret2 = function () {
console.log('Connecting to random peer', peerId);
var peerCount = _this7.webrtc.getPeers().length;
var peers = self.webrtc.getPeers(peerId, type);
peers.forEach(function (p) {
p.end(false);
peerCount--;
});
if (_this7.config.constraints.maxPeers > 0 && peerCount >= _this7.config.constraints.maxPeers) {
return {
v: void 0
};
}
peer = _this7.webrtc.createPeer({
id: peerId,
type: type,
enableDataChannels: _this7.config.enableDataChannels && type !== 'screen',
receiveMedia: {
offerToReceiveAudio: type !== 'screen' && !_this7.config.dataOnly && _this7.config.receiveMedia.offerToReceiveAudio ? 1 : 0,
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 {
connection.on('message', (message) => {
const peers = self.webrtc.getPeers(message.from, message.roomType);
const totalPeers = self.webrtc.getPeers().length;
let totalPeers = self.webrtc.getPeers().length;
let peer;
if (message.type === 'offer') {
if (peers.length) {
peers.forEach((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) {
return;
......@@ -404,7 +408,6 @@ class LioWebRTC extends WildEmitter {
let peer;
this.roomCount = Object.keys(roomDescription.clients).length;
// console.log(roomDescription);
this.id = roomDescription.you;
addNode(this.id);
this.unconnectivePeers[this.id] = true;
......@@ -413,7 +416,12 @@ class LioWebRTC extends WildEmitter {
for (type in client) {
if (type !== 'turnservers' && client[type]) {
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)) {
return;
}
......@@ -493,7 +501,12 @@ class LioWebRTC extends WildEmitter {
for (type in client) {
if (type !== 'turnservers' && client[type]) {
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) {
return;
}
......
Markdown is supported
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