@@ -22,6 +22,8 @@ io.use(router);
2222* Express-like routing capabilties for socket.io events.
2323* Gives you more control over how events are handled.
2424* Attach ` Router ` instances to other ` Router ` instances.
25+ * Support for "wildcard" (* ) and Regular Expression matching.
26+ * Event consumption and propagation.
2527
2628# Examples
2729
@@ -36,16 +38,32 @@ router.on(function (socket, args, next) {
3638 next ();
3739});
3840
39- // handles events named 'some event'
40- router .on (' some event' , function (socket , args , next ) {
41- assert .equal (args[0 ], ' some event' );
41+ // handles all events too
42+ router .on (' *' , function (socket , args , next ) {
43+ next ();
44+ });
45+
46+ // handles events matching 'some*'
47+ router .on (' some*' , function (socket , args , next ) {
48+ next ();
49+ });
50+
51+ // handles events matching '*events'
52+ router .on (' *event' , function (socket , args , next ) {
53+ next ();
54+ });
55+
56+ // handle events matching /^\w+/
57+ router .on (/ ^ \w + / , function (socket , args , next ) {
4258 next ();
4359});
4460
4561// handles all events
4662router .on (function (socket , args ) {
4763 // emits back to the client, and ends the chain.
4864 // Think `res.end()` for express.
65+ // calling `emit()` consumes the event which means no other handlers
66+ // get a chance to process it.
4967 socket .emit (args .shift (), args);
5068});
5169
@@ -58,14 +76,14 @@ var io = require('socket.io')(3000);
5876io .use (router);
5977```
6078
61- Here is an example of * not* handling a message and letting [ socket.io] ( https://github.com/Automattic/socket.io " socket.io ")
79+ Here is an example of * not* consuming the event and letting [ socket.io] ( https://github.com/Automattic/socket.io " socket.io ")
6280handle things * business as usual* .
6381
6482``` javascript
6583
6684var router = require (' socket.io-events' )();
6785router .on (function (socket , args , next ) {
68- // do something!
86+ // do something, but don't consume it.
6987 next ();
7088});
7189
@@ -213,6 +231,28 @@ var pretty = function (sock, args, next) { next() };
213231router.use(' chat' , chop, clean, pretty);
214232```
215233
234+ ### Router#use(event:RegExp, fn:Function, ...)
235+
236+ Bind the `function` using a `RegExp` pattern to match the `event`.
237+
238+ ```javascript
239+ router.use(/\w +/, function (sock, args, next) {
240+ assert.equal(args[0], ' chat' );
241+ args[1] = args[1].length > 128 ? args[1].slice(0, 125) + ' ... ' : args[1];
242+ next();
243+ });
244+ ```
245+
246+ You can also pass in multiple `function`s for handling the `event`.
247+
248+ ```javascript
249+ var chop = function (sock, args, next) { next() };
250+ var clean = function (sock, args, next) { next() };
251+ var pretty = function (sock, args, next) { next() };
252+
253+ router.use(/\w +/, chop, clean, pretty);
254+ ```
255+
216256### Router#use(router:Router, ...)
217257
218258You can attach another `Router` instance to your `Router` instance.
@@ -342,7 +382,3 @@ Tests are run using grunt. You must first globally install the grunt-cli with n
342382To run the tests, just run grunt
343383
344384 > grunt spec
345-
346- ## TODO
347-
348- 1 ) Support regex or some other kind of pattern matching other thang string literals
0 commit comments