summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGubolin <gubolin@fantasymail.de>2015-03-24 18:30:47 +0100
committerGubolin <gubolin@fantasymail.de>2015-03-24 18:30:47 +0100
commit315e01b93ae3f200cea44b086936e3d4d9ab7fc7 (patch)
tree5e28d5536ffcff8aadd7490ef4ed4dfd2a002c50
parentbdd780e0030507626c947f85880a9eaa36e24194 (diff)
parent121cf8b3658909f79571b621e9a9916c7f601dfc (diff)
downloadsnap-315e01b93ae3f200cea44b086936e3d4d9ab7fc7.tar.gz
snap-315e01b93ae3f200cea44b086936e3d4d9ab7fc7.zip
merge branch p2p
-rw-r--r--gui.js30
-rw-r--r--objects.js46
-rw-r--r--threads.js20
3 files changed, 43 insertions, 53 deletions
diff --git a/gui.js b/gui.js
index 79e9b02..56aa7bd 100644
--- a/gui.js
+++ b/gui.js
@@ -251,6 +251,36 @@ IDE_Morph.prototype.init = function (isAutoFill) {
setInterval(this.save, 1000 * 60 * 60 * 5); // every 5 minutes
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 a893cee..18825dc 100644
--- a/objects.js
+++ b/objects.js
@@ -4754,55 +4754,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;
-
- if (window.peers) {
- // I don't know why, but it works.
- window.peers.forEach(function (oldpeer) {
- if (id != oldpeer.id) {
- oldpeer.destroy()
- }
- });
- }
-
- this.peer = new Peer(id, {
- host: 'snapmesh.herokuapp.com',
- port: 443,
- secure: true,
- 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);
- }
- });
- 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);
- });
- });
- });
-
- window.peers.push(this.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 ba99cd7..fab636e 100644
--- a/threads.js
+++ b/threads.js
@@ -1324,6 +1324,8 @@ Process.prototype.doRemoveTemporaries = function () {
Process.prototype.sendPeerMessage = function (message, peer) {
var myself = this;
+ var stage = this.homeContext.receiver.parentThatIsA(StageMorph),
+ ide = this.homeContext.receiver.parentThatIsA(IDE_Morph);
if (peer instanceof List) {
peer.asArray().forEach(function (singlePeer) {
@@ -1331,10 +1333,12 @@ Process.prototype.sendPeerMessage = function (message, peer) {
});
return;
}
+ if (peer == ide.peer.id) {
+ stage.newPeerMessage(message, peer);
+ return;
+ }
- var stage = this.homeContext.receiver.parentThatIsA(StageMorph),
- ide = this.homeContext.receiver.parentThatIsA(IDE_Morph);
- 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") {
@@ -1352,11 +1356,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;
}
@@ -1366,8 +1370,8 @@ Process.prototype.reportPeerList = function () {
};
Process.prototype.reportPeerId = function () {
- var stage = this.homeContext.receiver.parentThatIsA(StageMorph);
- return stage.peerId;
+ var ide = this.homeContext.receiver.parentThatIsA(IDE_Morph);
+ return ide.peer.id;
};
// Process sprite inheritance primitives