Permalink
Browse files

Added some comments to the examples

  • Loading branch information...
1 parent e27124e commit 563017241950d5d95d907c58d83cad8dc5bc3be6 @jfd jfd committed Oct 21, 2013
Showing with 48 additions and 1 deletion.
  1. +13 −0 examples/helloworld.js
  2. +11 −1 examples/listener.js
  3. +11 −0 examples/multiplechannels.js
  4. +13 −0 examples/signals.js
@@ -1,11 +1,24 @@
var hydna = require('../index');
+
+// Open channel in "read/write" mode. All data sent to channel will
+// loop back from the remote connection.
var channel = hydna.createChannel('public.hydna.net', 'readwrite');
+// Connect handler, is triggered once that the channel is connected
channel.on('connect', function () {
+
+ // Connection to server is established. Send a "hello world"
+ // to public domain.
this.write('Hello world from NodeJS');
});
+// Data handler, is triggered each time DATA is received on the channel
channel.on('data', function (data) {
+
+ // Log data recived on channel.
console.log(data);
+
+ // Close the channel, which terminates the underlying
+ // receive-loop.
this.end();
});
View
@@ -1,11 +1,20 @@
var hydna = require('../index');
+
+// Open channel in "read" mode. We are just listening for data and signals.
var channel = hydna.createChannel('public.hydna.net', 'read');
+// Connect handler, is triggered once that the channel is connected
channel.on('connect', function (data) {
console.log("Press Ctrl-C to abort the receive loop");
- console.log("[WELCOME]: " + data);
+
+ // The "connect" handler may contain an optional welcome-message sent
+ // by remote part. Print message if exists.
+ if (typeof data == 'string') {
+ console.log("[WELCOME]: " + data);
+ }
});
+// Data handler, is triggered each time DATA is received on the channel
channel.on('data', function (data) {
if (typeof data == 'string') {
console.log('[DATA] ' + data);
@@ -14,6 +23,7 @@ channel.on('data', function (data) {
}
});
+// Signal handler, is triggered each time a SIGNAL is received on the channel
channel.on('signal', function (data) {
if (typeof data == 'string') {
console.log('[SIGNAL] ' + data);
@@ -2,19 +2,30 @@ var hydna = require('../index');
var messages = ['Hello', 'World'];
+// Open two channels in "readwrite" mode.
var channels = [
hydna.createChannel('public.hydna.net/channel1', 'rw'),
hydna.createChannel('public.hydna.net/channel2', 'rw')
];
+// Loop through both channels and add handlers.
channels.forEach(function (channel) {
+
+ // Connect handler, is triggered on both channels, once connected
channel.on('connect', function () {
console.log('Connected to channel "%s"', this.url);
+
+ // Write messages in correct order.
this.write(messages.shift());
});
+ // Data handler, is triggered on both channels, once DATA is received
channel.on('data', function (data) {
console.log('Channel "%s" recieved: %s', this.url, data);
+
+ // Close the channel. The application will terminate once both
+ // channels are closed.
this.end();
});
+
});
View
@@ -1,15 +1,28 @@
var hydna = require('../index');
+
+// Open channel in "emit" mode. We are requesting to "emit" signal to the
+// underlying behavior-layer.
var channel = hydna.createChannel('public.hydna.net/ping-back', 'emit');
+// Connect handler, is triggered once that the channel is connected
channel.on('connect', function (data) {
+
+ // Dispatch a signal with message "ping". The underlying behavior-layer
+ // will recieve it and can choose to take action
this.dispatch('ping');
});
+// Signal handler, is triggered each time a SIGNAL is received on the channel
channel.on('signal', function (data) {
+
+ // We received a signal on our disptach.
if (typeof data == "string") {
console.log('Received singal "%s"', data);
} else {
console.error("Expected a pong response");
}
+
+ // Close the channel, which terminates the underlying
+ // receive-loop.
this.end();
});

0 comments on commit 5630172

Please sign in to comment.