Socket.io超出范围或缺less方法?

我开始玩socket.io,我真的很喜欢看到一个更好的书面文档,因为很难find哪些方法有每个对象,callback如何工作等等。

所以我用express和socket.io来试试这个:

var app = require('express').createServer() , io = require('socket.io').listen(app); app.listen(8080); var clients = []; /*Routes*/ app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); app.get('/user/:id', function(req, res){ io.sockets.broadcast.emit('user', {id: req.params.id}); }); io.sockets.on('connection', function (socket) { clients.push(socket.id); io.sockets.socket(socket.id).emit('connection', clients); socket.broadcast.emit('conexion', { id: socket.id }); }); 

而当试图访问:本地主机:8080 /用户/testing它不起作用,并输出以下内容:

 TypeError: Cannot call method 'emit' of undefined at /var/www/node/app.js:11:26 at callbacks (/var/www/node/node_modules/express/lib/router/index.js:272:11) at param (/var/www/node/node_modules/express/lib/router/index.js:246:11) at param (/var/www/node/node_modules/express/lib/router/index.js:243:11) at pass (/var/www/node/node_modules/express/lib/router/index.js:253:5) at Router._dispatch (/var/www/node/node_modules/express/lib/router/index.js:280:4) at Object.handle (/var/www/node/node_modules/express/lib/router/index.js:45:10) at next (/var/www/node/node_modules/express/node_modules/connect/lib/http.js:203:15) at Object.handle (/var/www/node/node_modules/express/lib/http.js:84:5) at next (/var/www/node/node_modules/express/node_modules/connect/lib/http.js:203:15) 

所以,我的猜测是这个调用超出了范围,或者这个方法不存在或者什么。 我怎么能解决这个问题?

使用io.sockets.emit('user', {id: req.params.id}); 而不是io.sockets.broadcast.emit