如何将用户连接到套接字

我试图连接用户会话ID创build与每个用户login后创build的cookie连同它连接到套接字…任何方向或示例将是伟大的…我使用nodejs,express,redis,套接字。 io,并使用mongodb为我的分贝的东西的其余部分。

这里是app.js文件中的代码(但我不确定它的一些)

var connect = require('connect'); var RedisStore = require('connect-redis')(express); var redis = require("redis").createClient(); var io = require('socket.io'); var notificationsN = io.listen(server); app.use(express.session({ secret: "secret sauce", store: new RedisStore({ host: 'localhost', port: 3000, client: redis }) })); //Not sure if below is correct actually... need to connect it with the current user... how do i go about doing that? notificationsN.on('connection', function(client) { const subscribe = redis subscribe.subscribe('realtime'); subscribe.on('message', function(channel, message) { client.send(message); console.log('message received from ' + channel + " : " + message); }); client.on('message', function(msg) { console.log(msg); }); client.on('disconnect', function() { console.log('disconnecting from redis'); subscribe.quit(); }); }); 

这是客户:

 script(src='http://localhost:3000/socket.io/socket.io.js') script. var socket = io.connect('http://localhost/3000/'); socket.on('connect', function (data) { setStatus('connected'); socket.emit('subscribe', { channel: 'realtime' }); }); socket.on('reconnecting', function(data) { setStatus('reconnecting'); }); socket.on('message', function(data) { console.log('received a message: ', data); addMessage(data); }); function addMessage(data) { $('#notificationNumber').html(data); } function setStatus(msg) { console.log('connection status: ' + msg); } 

如果我正确理解你的问题,这应该是你的解决scheme: http : //www.danielbaulig.de/socket-ioexpress/