@@ -21,9 +21,12 @@ io.use(router);
2121* Easy to use interface for manipulating socket.io events.
2222* Express-like routing capabilties for socket.io events.
2323* Gives you more control over how events are handled.
24+ * Attach ` Router ` instances to other ` Router ` instances.
2425
2526# Examples
2627
28+ The method ` on ` is an alias to ` use ` .
29+
2730``` javascript
2831var assert = require (' assert' );
2932var router = require (' socket.io-events' )();
@@ -111,6 +114,206 @@ router.on(function (socket, args, next) {
111114io .use (router);
112115` ` `
113116
117+ You can even attach a ` Router' intance to another `Router` intance.
118+
119+ ```javascript
120+ var Router = require(' socket .io - events' )();
121+
122+ var a = Router();
123+ a.use(function (sock, args, next) { next() });
124+
125+ var b = Router();
126+ b.use(function (sock, args, next) { next() });
127+
128+ a.use(b)
129+
130+ var io = require(' socket .io ' )(3000);
131+ io.use(a);
132+ ```
133+
134+ # API
135+
136+ ## Router
137+
138+ Get the `Router` class.
139+
140+ ```javascript
141+ var Router = require(' socket .io - events' );
142+ ```
143+
144+ The `use` and `on` methods are equivalent. They also can be chained.
145+
146+ ```javascript
147+ var router = Router()
148+ .use(function (sock, args, next) { })
149+ .use(function (sock, args, next) { })
150+ .use(function (sock, args, next) { });
151+ ```
152+
153+ ### Router#()
154+
155+ Make a `Router` instance
156+
157+ ```javascript
158+ var router = Router();
159+ ```
160+
161+ ### Router#use(fn:Function, ...)
162+
163+ Attach a `function` to the router.
164+
165+ ```javascript
166+ router.use(function (sock, args, next) {
167+ //do something!
168+ next();
169+ });
170+ ```
171+
172+ You can pass in multiple `function`s.
173+
174+ ```javascript
175+ var a = function (sock, args, next) { next() };
176+ var b = function (sock, args, next) { next() };
177+ var c = function (sock, args, next) { next() };
178+
179+ router.use(a,b,c);
180+ ```
181+
182+ You can pass in a function that accepts an `Error` object.
183+
184+ ```javascript
185+ router.use(function (err, sock, args, next) {
186+ console.error(err);
187+
188+ //calling next(err) will invoke the next error handler.
189+ //to resume operation just call next()
190+ next(err);
191+ });
192+ ```
193+
194+ ### Router#use(event:String, fn:Function, ...)
195+
196+ Bind the `function` to the `event`.
197+
198+ ```javascript
199+ router.use(' chat' , function (sock, args, next) {
200+ assert.equal(args[0], ' chat' );
201+ args[1] = args[1].length > 128 ? args[1].slice(0, 125) + ' ... ' : args[1];
202+ next();
203+ });
204+ ```
205+
206+ You can also pass in multiple `function`s for handling the `event`.
207+
208+ ```javascript
209+ var chop = function (sock, args, next) { next() };
210+ var clean = function (sock, args, next) { next() };
211+ var pretty = function (sock, args, next) { next() };
212+
213+ router.use(' chat' , chop, clean, pretty);
214+ ```
215+
216+ ### Router#use(router:Router, ...)
217+
218+ You can attach another `Router` instance to your `Router` instance.
219+
220+ ```javascript
221+ var another = Router();
222+ another.use(function (sock, args, next) { next(); });
223+
224+ router.use(another);
225+ ```
226+
227+ Attach multiple routers in a single call.
228+
229+ ```javascript
230+ var foo = Router();
231+ foo.use(function (sock, args, next) { next(); });
232+
233+ var bar = Router();
234+ bar.use(function (sock, args, next) { next(); });
235+
236+ var baz = Router();
237+ baz.use(function (sock, args, next) { next(); });
238+
239+ router.use(foo, bar, baz);
240+ ```
241+
242+ ### Router#use(name:String, router:Router, ...)
243+
244+ Just like attaching a `function` to the router given the `event`. You can attach `Router`
245+ instance as well to the `event`.
246+
247+ ```javascript
248+ var foo = Router();
249+ foo.use(function (sock, args, next) { next(); });
250+
251+ router.use(' some event ' , foo);
252+ ```
253+
254+ Attach multiple routers in a single call to the `event` too.
255+
256+ ```javascript
257+ var foo = Router();
258+ foo.use(function (sock, args, next) { next(); });
259+
260+ var bar = Router();
261+ bar.use(function (sock, args, next) { next(); });
262+
263+ var baz = Router();
264+ baz.use(function (sock, args, next) { next(); });
265+
266+ router.use(' some event ' , foo, bar, baz);
267+ ```
268+
269+ ### Router#use(fns:Array, ...)
270+
271+ Attach an `Array` of `Fuction`' s or ` Router` instances, or an ` Array` or ` Array` s .
272+
273+ ` ` ` javascript
274+ var middleware = [
275+ function (sock, args, next) { next(); },
276+ [
277+ function (sock, args, next) { next(); },
278+ Router().use(function (sock, args, next) { next(); }),
279+ function (sock, args, next) { next(); },
280+ ],
281+ Router().use(function (sock, args, next) { next(); })
282+ ];
283+
284+ var errHandler = function (err, sock, args, next) { next(err); }
285+
286+ router.use(middleware, errHandler);
287+ ` ` `
288+
289+ ### Router#use (name: String , fns: Array , ... )
290+
291+ Attach everything to an event .
292+
293+ ` ` ` javascript
294+ var middleware = [
295+ function (sock, args, next) { next(); },
296+ [
297+ function (sock, args, next) { next(); },
298+ Router().use(function (sock, args, next) { next(); }),
299+ function (sock, args, next) { next(); },
300+ ],
301+ Router().use(function (sock, args, next) { next(); })
302+ ];
303+
304+ var errHandler = function (err, sock, args, next) { next(err); }
305+
306+ router.use('only this event', middleware, errHandler);
307+ ` ` `
308+
309+ ### Router#on (... )
310+
311+ This is an alias to to the ` use` method . It does the same thing.
312+
313+ ` ` ` javascript
314+ router.on(function (sock, args, next) { next() });
315+ ` ` `
316+
114317# Installation and Environment Setup
115318
116319Install node .js (See download and install instructions here: http: // nodejs.org/).
0 commit comments