summaryrefslogtreecommitdiff
path: root/objects.js
diff options
context:
space:
mode:
authorGubolin <gubolin@fantasymail.de>2015-03-06 20:26:02 +0100
committerGubolin <gubolin@fantasymail.de>2015-03-06 20:26:02 +0100
commitec6b7710d30dedb3b55a44bd24ecebf152ce4713 (patch)
tree6f9c12b619043d6ca4b500874216128f143d6692 /objects.js
parent168c328836c01d69418621921a50be5d2eea3039 (diff)
downloadsnap-yow-ec6b7710d30dedb3b55a44bd24ecebf152ce4713.tar.gz
snap-yow-ec6b7710d30dedb3b55a44bd24ecebf152ce4713.zip
first working version of p2p cloud variables
Diffstat (limited to 'objects.js')
-rw-r--r--objects.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/objects.js b/objects.js
index 1fe1309..71519f6 100644
--- a/objects.js
+++ b/objects.js
@@ -1176,6 +1176,19 @@ SpriteMorph.prototype.initBlocks = function () {
defaults: [localize('each item')]
},
+ // peer to peer communication
+ receivePeerMessage: {
+ type: 'hat',
+ category: 'other',
+ spec: 'when I receive %upvar from %upvar',
+ defaults: [localize('message'), localize('peer')]
+ },
+ sendPeerMessage: {
+ type: 'command',
+ category: 'other',
+ spec: 'send %s to %s'
+ },
+
// Code mapping - experimental
doMapCodeOrHeader: { // experimental
type: 'command',
@@ -2103,6 +2116,11 @@ SpriteMorph.prototype.blockTemplates = function (category) {
blocks.push('=');
+ blocks.push(block('receivePeerMessage'));
+ blocks.push(block('sendPeerMessage'));
+
+ blocks.push('=');
+
if (StageMorph.prototype.enableCodeMapping) {
blocks.push(block('doMapCodeOrHeader'));
blocks.push(block('doMapStringCode'));
@@ -3517,6 +3535,9 @@ SpriteMorph.prototype.allHatBlocksFor = function (message) {
if (morph.selector === 'receiveOnClone') {
return message === '__clone__init__';
}
+ if (morph.selector === 'receivePeerMessage') {
+ return message === '__peer__message__';
+ }
}
return false;
});
@@ -4386,8 +4407,62 @@ StageMorph.prototype.init = function (globals) {
this.acceptsDrops = false;
this.setColor(new Color(255, 255, 255));
this.fps = this.frameRate;
+
+ this.peer = new Peer({
+ host: 'snapmesh.herokuapp.com',
+ port: 80,
+ path: '/'
+ });
+ var myself = this;
+
+ this.peer.on('open', function (id) {
+ myself.peerId = id;
+ console.log('Id: ' + id); // DEBUG
+ });
+
+ this.peer.on('connection', function (connection) {
+ connection.on('open', function () {
+ connection.on('data', function (data) {
+ var hats = [];
+ // call hat blocks
+ myself.children.concat(myself).forEach(function (morph) {
+ if (morph instanceof SpriteMorph
+ || morph instanceof StageMorph) {
+ hats = hats.concat(
+ morph.allHatBlocksFor('__peer__message__'));
+ }
+ });
+ hats.forEach(function (block) {
+ var process = myself.threads.startProcess(block,
+ myself.isThreadSafe);
+ process.context.outerContext.variables.addVar('message');
+ process.context.outerContext.variables.setVar(
+ 'message',
+ data
+ );
+ process.context.outerContext.variables.addVar('peer');
+ process.context.outerContext.variables.setVar(
+ 'peer',
+ connection.peer
+ );
+ });
+ });
+ });
+ });
};
+// peer to peer communication
+
+SpriteMorph.prototype.sendPeerMessage = function (message, peer) {
+ var stage = this.parentThatIsA(StageMorph);
+ var connection = stage.peer.connect(peer, {reliable: true});
+ connection.on('open', function () {
+ connection.send(message);
+ });
+};
+
+StageMorph.prototype.sendPeerMessage = SpriteMorph.prototype.sendPeerMessage;
+
// StageMorph scaling
StageMorph.prototype.setScale = function (number) {
@@ -5305,6 +5380,10 @@ StageMorph.prototype.blockTemplates = function (category) {
blocks.push('=');
+ blocks.push(block('receivePeerMessage'));
+ blocks.push(block('sendPeerMessage'));
+ blocks.push('=');
+
if (StageMorph.prototype.enableCodeMapping) {
blocks.push(block('doMapCodeOrHeader'));
blocks.push(block('doMapStringCode'));