Permalink
Browse files

Review examples

  • Loading branch information...
1 parent b3bb6e5 commit 2f846f7b36ee7064ec0139651a3270830a079d7e @tedeh committed Jun 30, 2012
View
@@ -1,33 +0,0 @@
-#!/usr/bin/env node
-
-var fs = require('fs');
-var path = require('path');
-var util = require('util');
-
-var program = require('commander');
-var eyes = require('eyes');
-var uglify = require('uglify-js');
-
-program.version('1.0.0')
- .parse(process.argv);
-
-var targets = [
- { in: 'lib/client/jquery.js', out: 'build/jquery.jayson.js' },
- { in: 'lib/client/jquery.js', out: 'build/jquery.jayson.min.js', minify: true }
-];
-
-console.log();
-
-targets.forEach(function(target) {
- var str = fs.readFileSync(target.in, 'utf8');
- if(target.minify) {
- var ast = uglify.parser.parse(str);
- ast = uglify.uglify.ast_mangle(ast);
- ast = uglify.uglify.ast_squeeze(ast);
- str = uglify.uglify.gen_code(ast);
- }
- fs.writeFileSync(target.out, str, 'utf8');
- console.log(" wrote " + eyes.stylize('%s', 'magenta', {}), target.out);
-});
-
-console.log();
@@ -0,0 +1,24 @@
+var jayson = require(__dirname + '/../..');
+
+var server = jayson.server({
+ add: function(a, b, callback) {
+ return callback(null, a + b);
+ }
+});
+
+// "http" will be an instance of require('http').Server
+var http = server.http();
+
+// "https" will be an instance of require('https').Server
+var https = server.https({
+ //cert: require('fs').readFileSync('cert.pem'),
+ //key require('fs').readFileSync('key.pem')
+});
+
+http.listen(80, function() {
+ console.log('Listening on *:80')
+});
+
+https.listen(443, function() {
+ console.log('Listening on *:443')
+});
@@ -0,0 +1,12 @@
+var jayson = require(__dirname + '/../..');
+
+var client = jayson.client.http({
+ host: 'localhost',
+ port: 3000
+});
+
+// the third parameter is set to "null" to indicate a notification
+client.request('ping', [], null, function(err) {
+ if(err) throw err;
+ // request was received successfully
+});
@@ -0,0 +1,10 @@
+var jayson = require(__dirname + '/../..');
+
+var server = jayson.server({
+ ping: function(callback) {
+ // do something
+ callback();
+ }
+});
+
+server.http().listen(3000);
@@ -5,6 +5,7 @@ var client = jayson.client.http({
port: 3000 // the port of the public server
});
-client.request('add', [5, 9], function(err, result) {
- console.log('result', result);
+client.request('add', [5, 9], function(err, error, result) {
+ if(err) throw err;
+ console.log(result); // 14!
});
@@ -7,4 +7,6 @@ var server = jayson.server({
});
// let the private server listen to localhost:3001
-server.http().listen(3001);
+server.http().listen(3001, '127.0.0.1', function() {
+ console.log('Listening on 127.0.0.1:3001');
+});
@@ -9,4 +9,6 @@ var server = jayson.server({
});
// let the server listen to *:3000
-server.http().listen(3000, '0.0.0.0');
+server.http().listen(3000, function() {
+ console.log('Listening on *:3000');
+});
@@ -0,0 +1,21 @@
+var jayson = require(__dirname + '/../..');
+var shared = require('./shared');
+
+// create a client with the shared reviver and replacer
+var client = jayson.client.http({
+ port: 3000,
+ hostname: 'localhost',
+ reviver: shared.reviver,
+ replacer: shared.replacer
+});
+
+// create the object
+var instance = new shared.Counter(2);
+
+// invoke "increment"
+client.request('increment', [instance], function(err, error, result) {
+ if(err) throw err;
+ console.log(result instanceof shared.Counter); // true
+ console.log(result.count); // 3!
+ console.log(instance === result); // false - it won't be the same object, naturally
+});
@@ -0,0 +1,19 @@
+var jayson = require(__dirname + '/../..');
+var shared = require('./shared');
+
+// Set the reviver/replacer options
+var options = {
+ reviver: shared.reviver,
+ replacer: shared.replacer
+};
+
+// create a server
+var server = jayson.server({
+ increment: function(counter, callback) {
+ counter.increment();
+ callback(null, counter);
+ }
+}, options);
+
+// let the server listen to for http connections on localhost:3000
+server.http().listen(3000);
@@ -0,0 +1,23 @@
+var Counter = exports.Counter = function(value) {
+ this.count = value || 0;
+};
+
+Counter.prototype.increment = function() {
+ this.count += 1;
+};
+
+exports.replacer = function(key, value) {
+ if(value instanceof Counter) {
+ return {$class: 'counter', $props: {count: value.count}};
+ }
+ return value;
+};
+
+exports.reviver = function(key, value) {
+ if(value && value.$class === 'counter') {
+ var obj = new Counter;
+ for(var prop in value.$props) obj[prop] = value.$props[prop];
+ return obj;
+ }
+ return value;
+};
@@ -0,0 +1,13 @@
+var jayson = require(__dirname + '/../..');
+
+// create a client
+var client = jayson.client.http({
+ port: 3000,
+ hostname: 'localhost'
+});
+
+// invoke "add"
+client.request('add', [1, 1], function(err, error, response) {
+ if(err) throw err;
+ console.log(response); // 2!
+});
@@ -0,0 +1,11 @@
+var jayson = require(__dirname + '/../..');
+
+// create a server
+var server = jayson.server({
+ add: function(a, b, callback) {
+ callback(null, a + b);
+ }
+});
+
+// Bind a http interface to the server and let it listen to localhost:3000
+server.http().listen(3000);

0 comments on commit 2f846f7

Please sign in to comment.