Skip to content

Commit ad060d4

Browse files
committed
fixed issue with consumption and added an example
1 parent 58976ff commit ad060d4

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

examples/consume.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var ok = require('assert').equal;
2+
3+
var router = require('./..')();
4+
router.on(function (socket, args, next) {
5+
//do something!
6+
next();
7+
});
8+
router.on(function (sock, args, next) {
9+
sock.emit('what', new Date);
10+
// this next won't do us any good the emit was already called.
11+
next();
12+
setTimeout(function () {
13+
sock.emit('what', new Date);
14+
}, 1000);
15+
sock.emit('what', new Date);
16+
});
17+
18+
var io = require('socket.io')(3000);
19+
io.use(router);
20+
io.on('connection', function (socket) {
21+
// this won't get called because the "echo" event will be consumed!
22+
socket.on('echo', function (data) {
23+
socket.emit('echo', data);
24+
});
25+
});
26+
27+
setTimeout(function () {
28+
var client = require('socket.io-client').connect('ws://localhost:3000');
29+
client.on('connect', function () {
30+
client.emit('echo', 'data');
31+
});
32+
client.on('echo', function (data) {
33+
ok(data,'data');
34+
console.log('we good');
35+
process.exit();
36+
});
37+
var c = 0;
38+
client.on('what', function (data) {
39+
if (++c >= 3) {
40+
console.log('we good');
41+
process.exit();
42+
}
43+
});
44+
},1000);

lib/router.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ function Router () {
103103
debug('unwrapping the socket');
104104
socket.emit = socket.emit.emit;
105105
}
106+
107+
socket.emit.done = true;
106108

107109
emit.apply(socket, args);
108110
});
@@ -113,6 +115,8 @@ function Router () {
113115

114116
(function step (err) {
115117

118+
if (socket.emit.done) return;
119+
116120
debug('current step %s of %s', i+1, len);
117121

118122
function next (err) {
@@ -126,6 +130,7 @@ function Router () {
126130
}
127131
}
128132
else {
133+
if (socket.emit.done) return;
129134
if ('function' === typeof cb) {
130135
cb(null, socket, args);
131136
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "socket.io-events",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Power your socket.io apps with express like event routing.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)