diff options
| -rw-r--r-- | gui.js | 31 | ||||
| -rw-r--r-- | objects.js | 33 | ||||
| -rw-r--r-- | threads.js | 14 |
3 files changed, 38 insertions, 40 deletions
@@ -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) { @@ -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; @@ -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 |
