访问路由端点之外的req属性

var express = require('express'); var router = module.exports = express.Router(); var server = require("http").Server(express); var io = require("socket.io")(server); server.listen(5000); //how to get my user.id here? router.get('/', function(req, res) { io.on('connection', function(client) { client.on('order_'+req.user.id, function(data) { io.emit('order_'+req.user.id,data); }); }); }); 

上面的代码运行良好,但它有一个问题。 如果用户刷新页面/或重新进入页面,连接会相乘。 我应该把router.get('/'..以外,但我必须使用req.user.id

任何想法我怎么能得到user.id?

正如我已经提到你不需要处理这个,因为在刷新客户端closures旧的连接和新的页面加载应该再次连接,所以没有多个连接

我的样品

 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendfile('index.html'); }); io.on('connection', function(socket){ console.log('CONNECTED!'); socket.on('disconnect', function () { console.log('DISCONNECTED!'); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); 

只刷新几次标签,你会得到服务器上的以下输出:

 CONNECTED! DISCONNECTED! CONNECTED! DISCONNECTED! CONNECTED! ... 

传递用户标识

无论如何,这应该通过将用户标识信息从客户端传递到服务器来完成,您可以通过为其定义新事件来完成,例如通过authenticated

 socket.on('authenticated', function(userId) { socket.on('disconnect', function () { console.log(userId + ' DISCONNECTED!'); }); }); 

或者将其作为附加数据传递给每个事件。

 socket.on('someEvent', function(someData, userId) { //... }); 

替代可以使用像session.socket.io模块

 sessionSockets.on('connection', function (err, socket, session) { //your regular socket.io code goes here //and you can still use your io object }); 

从路线发射事件

为了从路由发出事件,你需要在expresssocket.io之间有某种连接,可以通过像这样的映射来完成:

 var sockets = {}; socket.on('authenticated', function(userId) { sockets[userId] = socket; ... }); 

和内部路由只需使用sockets[userId].emit发射事件。

参考

你需要有逻辑(Express和socket.io单独)。 为了得到用户的ID我build议你这种方法:

 // file - app.js // In your middleware authorization or another middleware // you have to pass the user to the response. app.use(function(req, res, next){ res.locals['user'] = req.user; }); ... var io = require('socket.io').listen(server); require('./sockets_controller.js')(io); 

现在让我们来实现sockets_controller.js

 global.clients = {}; // Make a global object - so you can use it everywhere module.exports = function(io){ io.on('connection', function(socket){ socket.on('get_id_user', function(data){ if (data){ if (!data.id_user) return; this.id_user = data.id_user; // One client can have more than one socket opened!! if (clients[this.id_user]){ clients[this.id_user][this.id] = this; } else { clients[this.id_user] = {}; clients[this.id_user][this.id] = this; } } }); socket.on('disconnect', function(){ if (clients[this.id_user]){ delete clients[this.id_user][this.id]; if(Object.keys(clients[this.id_user]).length == 0){ delete clients[this.id_user]; } } }); }; 

现在为你的客户端(让我把它的JADE你的渲染引擎):

 script. const id_user = '#{user.id}', io = io.connect("..."); io.on('connect', function(socket){ if(id_user != 'undefined') io.emit('get_id_user', { id_user: id_user }); }); 

有了这个,你可以从其他地方为特定用户获取套接字。 例如在另一个路线。

 app.post('/whatever', function(req, res){ for(var socketID in clients[req.user.id_user]){ clients[req.user.id_user][socketID].emit(...); } });