From e3f7a47d95711effb8ab4b95b1c5fdde9178a72a Mon Sep 17 00:00:00 2001 From: Gubolin Date: Sun, 8 Mar 2015 15:15:29 +0100 Subject: destroy old peers properly --- objects.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index 282d01f..3a39a5c 100644 --- a/objects.js +++ b/objects.js @@ -4428,11 +4428,9 @@ StageMorph.prototype.initPeering = function (id) { if (window.peers) { // I don't know why, but it works. - window.peers.forEach(function (oldpeer) { - if (id != oldpeer.id) { - oldpeer.destroy() - } - }); + while (window.peers.length > 0) { + window.peers.pop().destroy(); + } } this.peer = new Peer(id, { -- cgit v1.3.1 From 916dfe65e4afe636ce42e178fe88815b4b86026d Mon Sep 17 00:00:00 2001 From: Gubolin Date: Sun, 8 Mar 2015 16:13:29 +0100 Subject: another attempt to fix disconnect issues --- objects.js | 13 ++++--------- threads.js | 6 ++---- 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index 3a39a5c..b115b3b 100644 --- a/objects.js +++ b/objects.js @@ -4426,11 +4426,9 @@ StageMorph.prototype.init = function (globals) { StageMorph.prototype.initPeering = function (id) { var myself = this; - if (window.peers) { + if (window.peer) { // I don't know why, but it works. - while (window.peers.length > 0) { - window.peers.pop().destroy(); - } + window.peer.destroy(); } this.peer = new Peer(id, { @@ -4440,13 +4438,10 @@ StageMorph.prototype.initPeering = function (id) { path: '/' }); - this.peer.on('open', function (id) { - myself.peerId = id; - }); this.peer.on('disconnected', function () { // peer.reconnect does not work (?) because 'id' is undefined if (!myself.peer.destroyed) { - myself.initPeering(myself.peerId); + myself.initPeering(myself.peer.id); } }); this.peer.on('error', function (err) { @@ -4461,7 +4456,7 @@ StageMorph.prototype.initPeering = function (id) { }); }); - window.peers.push(this.peer); + window.peer = this.peer; }; StageMorph.prototype.newPeerMessage = function (data, peer) { diff --git a/threads.js b/threads.js index 165c7fc..d8ec04f 100644 --- a/threads.js +++ b/threads.js @@ -1302,15 +1302,13 @@ Process.prototype.sendPeerMessage = function (message, peer) { var stage = this.homeContext.receiver.parentThatIsA(StageMorph), ide = this.homeContext.receiver.parentThatIsA(IDE_Morph); - console.log(peer, stage.peerId); if (peer instanceof List) { peer.asArray().forEach(function (singlePeer) { myself.sendPeerMessage(message, singlePeer); }); return; } - if (peer == stage.peerId) { - console.log('hello self!'); + if (peer == stage.peer.id) { stage.newPeerMessage(message, peer); return; } @@ -1348,7 +1346,7 @@ Process.prototype.reportPeerList = function () { Process.prototype.reportPeerId = function () { var stage = this.homeContext.receiver.parentThatIsA(StageMorph); - return stage.peerId; + return stage.peer.id; }; // Process lists primitives -- cgit v1.3.1 From 9163b3ca714086e1877249bf38e47040bd1b3e67 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Sun, 8 Mar 2015 16:32:36 +0100 Subject: solve connection issues --- objects.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index b115b3b..e25f119 100644 --- a/objects.js +++ b/objects.js @@ -4426,11 +4426,6 @@ StageMorph.prototype.init = function (globals) { StageMorph.prototype.initPeering = function (id) { var myself = this; - if (window.peer) { - // I don't know why, but it works. - window.peer.destroy(); - } - this.peer = new Peer(id, { host: 'snapmesh.herokuapp.com', port: 443, @@ -4439,9 +4434,10 @@ StageMorph.prototype.initPeering = function (id) { }); this.peer.on('disconnected', function () { - // peer.reconnect does not work (?) because 'id' is undefined - if (!myself.peer.destroyed) { - myself.initPeering(myself.peer.id); + if (!myself.peer.destroyed && myself.peer.id) { + myself.peer.reconnect(); + } else { + myself.initPeering(); } }); this.peer.on('error', function (err) { @@ -4455,8 +4451,6 @@ StageMorph.prototype.initPeering = function (id) { }); }); }); - - window.peer = this.peer; }; StageMorph.prototype.newPeerMessage = function (data, peer) { -- cgit v1.3.1 From 121cf8b3658909f79571b621e9a9916c7f601dfc Mon Sep 17 00:00:00 2001 From: Gubolin Date: Fri, 20 Mar 2015 20:53:20 +0100 Subject: create one peer per IDE --- gui.js | 31 ++++++++++++++++++++++++++++++- objects.js | 33 +-------------------------------- threads.js | 14 +++++++------- 3 files changed, 38 insertions(+), 40 deletions(-) (limited to 'objects.js') diff --git a/gui.js b/gui.js index 1f51405..543f776 100644 --- a/gui.js +++ b/gui.js @@ -246,7 +246,36 @@ IDE_Morph.prototype.init = function (isAutoFill) { // override inherited properites: this.color = this.backgroundColor; - window.peers = []; + this.createPeer(); +}; + +IDE_Morph.prototype.createPeer = function() { + var myself = this; + this.peer = new Peer(this.peerId, { + host: 'snapmesh.herokuapp.com', + port: 443, + secure: true, + path: '/' + }); + + this.peer.on('open', function (id) { + myself.peerId = id; + }); + this.peer.on('disconnected', function () { + myself.peer.destroy(); + myself.createPeer(); + }); + this.peer.on('error', function (err) { + console.log(err); // DEBUG + }); + + this.peer.on('connection', function (connection) { + connection.on('open', function () { + connection.on('data', function (data) { + myself.stage.newPeerMessage(data, connection.peer); + }); + }); + }); }; IDE_Morph.prototype.openIn = function (world) { diff --git a/objects.js b/objects.js index e25f119..3859295 100644 --- a/objects.js +++ b/objects.js @@ -4419,42 +4419,11 @@ StageMorph.prototype.init = function (globals) { this.acceptsDrops = false; this.setColor(new Color(255, 255, 255)); this.fps = this.frameRate; - - this.initPeering(); -}; - -StageMorph.prototype.initPeering = function (id) { - var myself = this; - - this.peer = new Peer(id, { - host: 'snapmesh.herokuapp.com', - port: 443, - secure: true, - path: '/' - }); - - this.peer.on('disconnected', function () { - if (!myself.peer.destroyed && myself.peer.id) { - myself.peer.reconnect(); - } else { - myself.initPeering(); - } - }); - this.peer.on('error', function (err) { - console.log(err); // DEBUG - }); - - this.peer.on('connection', function (connection) { - connection.on('open', function () { - connection.on('data', function (data) { - myself.newPeerMessage(data, connection.peer); - }); - }); - }); }; StageMorph.prototype.newPeerMessage = function (data, peer) { var ide = this.parentThatIsA(IDE_Morph); + if (!ide || !peer) return; var myself = this; var hats = [], model, message; diff --git a/threads.js b/threads.js index d8ec04f..573b7fe 100644 --- a/threads.js +++ b/threads.js @@ -1308,12 +1308,12 @@ Process.prototype.sendPeerMessage = function (message, peer) { }); return; } - if (peer == stage.peer.id) { + if (peer == ide.peer.id) { stage.newPeerMessage(message, peer); return; } - var connection = stage.peer.connect(peer, {reliable: true}); + var connection = ide.peer.connect(peer, {reliable: true}); connection.on('open', function () { var data; if (typeof message == "string") { @@ -1331,11 +1331,11 @@ Process.prototype.reportPeerList = function () { var myself = this; if (!this.context.wait) { - var stage = this.homeContext.receiver.parentThatIsA(StageMorph); - stage.peer.listAllPeers(function (peers) { + this.context.wait = true; + var ide = this.homeContext.receiver.parentThatIsA(IDE_Morph); + ide.peer.listAllPeers(function (peers) { myself.context.result = new List(peers); }); - this.context.wait = true; } else if (this.context.result) { return this.context.result; } @@ -1345,8 +1345,8 @@ Process.prototype.reportPeerList = function () { }; Process.prototype.reportPeerId = function () { - var stage = this.homeContext.receiver.parentThatIsA(StageMorph); - return stage.peer.id; + var ide = this.homeContext.receiver.parentThatIsA(IDE_Morph); + return ide.peer.id; }; // Process lists primitives -- cgit v1.3.1