Skip to content

Commit 58976ff

Browse files
committed
Merge pull request #4 from turbonetix/add_wildcard
READY: Add wildcard
2 parents 8f3acb0 + a947ae6 commit 58976ff

File tree

6 files changed

+188
-78
lines changed

6 files changed

+188
-78
lines changed

README.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4662
router.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);
5876
io.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")
6280
handle things *business as usual*.
6381

6482
```javascript
6583

6684
var router = require('socket.io-events')();
6785
router.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() };
213231
router.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
218258
You 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
342382
To 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

examples/multiple.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1+
var debug = require('debug')('router');
12
var ok = require('assert').equal;
23
var Router = require('./..');
34

45
var a = Router();
56
a.on('say', function (sock, args, next) {
7+
debug('World');
68
args.push('World');
79
next();
810
});
911

1012
var b = Router();
1113
b.use('say', function (sock, args, next) {
14+
debug('Good');
1215
args.push('Good');
1316
next();
1417
});
1518

1619
var c = Router();
1720
c.use('say', function (sock, args, next) {
21+
debug('Bye');
1822
args.push('Bye');
1923
next();
2024
});
2125

2226
var d = Router();
2327
d.use(function (sock, args, next) {
28+
debug('!!!');
2429
args.push('!!!');
2530
next();
2631
});
@@ -33,6 +38,7 @@ var io = require('socket.io')(3000);
3338
io.use(a);
3439
io.on('connection', function (sock) {
3540
sock.on('say', function (hello, world, good, bye, exclamation) {
41+
debug('the socket emit function????', sock.emit.toString());
3642
sock.emit('say', hello, world, good, bye, exclamation);
3743
});
3844
});

0 commit comments

Comments
 (0)