Skip to content

Commit 5bc92da

Browse files
committed
Merge pull request #3 from turbonetix/modify_how_router_attaches
READY: Modify how router attaches
2 parents f47fa82 + 34eb696 commit 5bc92da

File tree

7 files changed

+584
-138
lines changed

7 files changed

+584
-138
lines changed

README.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2831
var assert = require('assert');
2932
var router = require('socket.io-events')();
@@ -111,6 +114,206 @@ router.on(function (socket, args, next) {
111114
io.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

116319
Install node.js (See download and install instructions here: http://nodejs.org/).

examples/multiple.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var ok = require('assert').equal;
2+
var Router = require('./..');
3+
4+
var a = Router();
5+
a.on('say', function (sock, args, next) {
6+
args.push('World');
7+
next();
8+
});
9+
10+
var b = Router();
11+
b.use('say', function (sock, args, next) {
12+
args.push('Good');
13+
next();
14+
});
15+
16+
var c = Router();
17+
c.use('say', function (sock, args, next) {
18+
args.push('Bye');
19+
next();
20+
});
21+
22+
var d = Router();
23+
d.use(function (sock, args, next) {
24+
args.push('!!!');
25+
next();
26+
});
27+
28+
a.use(b)
29+
b.use(c);
30+
c.use(d);
31+
32+
var io = require('socket.io')(3000);
33+
io.use(a);
34+
io.on('connection', function (sock) {
35+
sock.on('say', function (hello, world, good, bye, exclamation) {
36+
sock.emit('say', hello, world, good, bye, exclamation);
37+
});
38+
});
39+
40+
setTimeout(function () {
41+
var sock = require('socket.io-client').connect('ws://localhost:3000');
42+
sock.on('connect', function () {
43+
sock.emit('say', 'Hello');
44+
});
45+
sock.on('say', function (hello, world, good, bye, exclamation) {
46+
ok(hello, 'Hello');
47+
ok(world, 'World');
48+
ok(good, 'Good');
49+
ok(bye, 'Bye');
50+
ok(exclamation, '!!!');
51+
console.log('we good');
52+
process.exit(0);
53+
});
54+
55+
}, 1000);

examples/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ echo 'testing bau'
55
node bau
66
echo 'testing recover'
77
node recover
8+
echo 'testing routers attached to routers'
9+
node multiple

0 commit comments

Comments
 (0)