Skip to content
Browse files

Added README and method for catching unknown methods

  • Loading branch information...
1 parent 9437240 commit c61ba7413042cca2a636f2e4f89b2d34bf6500c4 NeoXiD committed Feb 2, 2013
Showing with 83 additions and 1 deletion.
  1. +70 −0 README.md
  2. +4 −0 lib/protocols/v1.js
  3. +8 −0 lib/server.js
  4. +1 −1 package.json
View
70 README.md
@@ -0,0 +1,70 @@
+# iowamp #
+
+iowamp is a WAMP™ server in NodeJS. Currently it only supports basic RPC calls, but pub/sub support is coming. It attaches to [WebSocket.IO](http://github.com/learnboost/websocket.io).
+
+## License ##
+Apache License (version 2)
+
+## Prerequisites ##
+iowamp itselfs requires version 0.6.x of NodeJS or higher. If you want to run the tests, you'll want Vows.
+
+To really use iowamp, you will also need websocket.io
+
+## Installing with [NPM](http://npmjs.org) ##
+```
+npm install modelize
+```
+
+## Attach iowamp to a websocket.io server ##
+Before you can start using iowamp in your project, you need to attach it to an websocket.io instance of your choice:
+
+```javascript
+var iowamp = require('./lib'),
+ wsio = require('websocket.io');
+
+var server = wsio.listen(8000);
+var app = iowamp.attach(server);
+```
+
+## Register a RPC class with methods ##
+Registering a RPC class with some methods is also easy - here is an example for it:
+
+```javascript
+var iowamp = require('./lib'),
+ wsio = require('websocket.io');
+
+var server = wsio.listen(8000);
+var app = iowamp.attach(server);
+
+app.rpc('http://example.com/calc#', function() {
+ this.register('add', function(cb, a, b) {
+ cb(null, a + b);
+ });
+});
+```
+
+The code should be kinda self-explanatory, allthough here is some additional information:
+
+**app.rpc(baseURI, constructor)** registers a new RPC class
+
+- *baseURI* The base URI for the class. It must be a complete URI and it should end with a #. (A CURIE / compact URI is not allowed)
+- *constructor* Should be a function which registers some RPC methods (will get called in the iowamp scope)
+
+**this.register(name, method)**
+
+- *name* The name of the method. To call the method in a WAMP client, you would need to specifiy the baseURI and the method name like here: http://example.com/calc#add
+- *method* The method which should be called. The first parameter is always the callback function, followed by the arguments passed from the WAMP client.
+
+**cb(error, result)** Kinda self-explanatory. If an error will be passed, a generic error will be send back to the WAMP client. If not, the result will be send back.
+
+## Catching unknown methods ##
+If you want to catch methods which are unknown / not declared, you can listen for the *unknownCall* event:
+
+```javascript
+app.on('unknownCall', function(baseURI, method, callback, args...) {
+// Your code goes here
+});
+```
+
+- - -
+iowamp NodeJS WAMP™ server - © 2012-2013 P. Mathis (dev@snapserv.net)
View
4 lib/protocols/v1.js
@@ -80,8 +80,12 @@ handlers.CALL = function(server, client, callID, procURI) {
if(rpcClass.hasOwnProperty(procURI.methodURI)) {
var rpcMethod = rpcClass[procURI.methodURI];
rpcMethod.apply(null, [cb].concat(args));
+ return;
}
}
+
+ // Class or method is unknown
+ server.emit('unknownCall', procURI.baseURI, procURI.methodURI, [cb].concat(args));
};
/**
View
8 lib/server.js
@@ -106,6 +106,14 @@ Server.prototype.rpc = function(baseURI, rpcClass) {
}
};
+ // Validate parameters
+ var regexBaseURI = /^(http|https):\/\/\S+\/[\w_-]+#$/;
+ if(!regexBaseURI.test(baseURI))
+ throw new Error('Invalid URI specified. (CURIE is not allowed)');
+ if(!rpcClass || rpcClass.constructor != Function)
+ throw new Error('No constructor for RPC class specified');
+
+ // Create a new RPC class
this.rpcClasses[baseURI] = {};
rpcClass.apply(rpcClassConstructor);
}
View
2 package.json
@@ -14,7 +14,7 @@
"io",
"wamp"
],
- "version": "0.1.0",
+ "version": "0.1.1",
"author": "Pascal Mathis <dev@snapserv.net> (http://snapserv.net)",
"main": "./lib/index.js",
"repository": {

0 comments on commit c61ba74

Please sign in to comment.
Something went wrong with that request. Please try again.