Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- all user events call the callback with two parameters (the event da…

…ta and the ID of the event emiter instance)
  • Loading branch information...
commit 77c82daac3714c6507365e66fdd46cd903efa9a2 1 parent 9c8763e
@digitalwm authored
Showing with 26 additions and 17 deletions.
  1. +7 −16 cloudjs.js
  2. +18 −0 example.js
  3. +1 −1  package.json
View
23 cloudjs.js
@@ -388,12 +388,7 @@ Clouder.prototype.connect = function () {
setInterval(this.sendHeartbeat, this._heartbeat, this);
//noinspection JSUnresolvedFunction
- this.on("heartbeat", function (data) {
- var sid, score;
-
- sid = data[1];
- score = data[0];
-
+ this.on("heartbeat", function (score, sid) {
if(sid === self.id) {
self.cleanPeer();
}
@@ -402,12 +397,7 @@ Clouder.prototype.connect = function () {
}
});
- this.on("moveItem", function (data) {
- var obj, timer;
-
- obj = data[0];
- timer = data[1];
-
+ this.on("moveItem", function (obj, timer) {
self.addElementToPool(obj, timer);
});
@@ -463,25 +453,26 @@ Clouder.prototype.connect = function () {
if(msg.type === 1) {
if(msg.title.toString() === "heartbeat") {
//noinspection JSUnresolvedFunction
- self.emit("heartbeat", [msg.body,msg.sid]);
+ self.emit("heartbeat", msg.body,msg.sid);
}
else if(msg.title.toString() === "moveItem") {
bodyParser = serializer.parse(msg.body);
if(bodyParser.did.toString() === self.id.toString()) {
console.log("Received item from : " + msg.sid);
//noinspection JSUnresolvedFunction
- self.emit("moveItem", [bodyParser.obj, bodyParser.timer]);
+ self.emit("moveItem", bodyParser.obj, bodyParser.timer);
}
}
}
else if(msg.type === 2) {
if(msg.bounce === true) {
//noinspection JSUnresolvedFunction
- self.emit(msg.title, [msg.body, msg.sid]);
+ self.emit(msg.title, msg.body, msg.sid);
}
else {
if(msg.sid.toString() !== self.id.toString()) {
- self.emit(msg.title, [msg.body, msg.sid]);
+ //noinspection JSUnresolvedFunction
+ self.emit(msg.title, msg.body, msg.sid);
}
}
}
View
18 example.js
@@ -22,6 +22,10 @@ cloudConfig = {
var myObject = new cloud.Clouder(11211, "255.255.255.255", cloudConfig);
myObject.connect();
+/*
+ Object pool
+ */
+
function guidGenerator() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
@@ -43,6 +47,20 @@ for(i = 0 ; i < numberOfObjects ; i++) {
myObject.addElementToPool(new Ob1(guidGenerator()), 5000);
}
+/*
+ Event fire and forget
+ */
+
+myObject.on("test", function(data, sid) {
+ console.log("received " + data + " from " + sid);
+});
+
+myObject.send("test","test2");
+
+/*
+ Event with callbacks
+ */
+
function mesgCallback1(data, sid) {
console.log("Received callback from " + sid + ", time to return data");
return "test" + sid;
View
2  package.json
@@ -2,7 +2,7 @@
"author" : "Dan Harabagiu <harabagiu.dan@gmail.com> (http://dan.harabagiu.net/)",
"name" : "cloudjs",
"description" : "A network distributed event system. Similar to node JS standard event system. A process pool, where objects can be added and ran at a periodic interval a predefined functions. An auto-balancing system, that migrate objects in the process pool, from one running instance to another, based on the load of each instance.",
- "version" : "0.0.8",
+ "version" : "0.0.9",
"homepage" : "http://dan.harabagiu.net/cloudjs",
"keywords" : ["udp", "broadcast", "realtime", "cloud", "event"],
"repository" : {
Please sign in to comment.
Something went wrong with that request. Please try again.