summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gui.js31
-rw-r--r--objects.js33
-rw-r--r--threads.js14
3 files changed, 38 insertions, 40 deletions
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