我正在尝试构建一个实时仪表板,将数据从mongodb上限集合流式传输到任何用户登录的前端。后端将是python或类似的,并将持续更新用户集合中的新数据。我使用passportjs进行身份验证,并使用passport-socketio-redis来集成socketio。
我认为第一个目标是将用户名作为值添加到socketio客户端列表中。
示例:一个客户端已连接,客户端列表为:['wukBbRD3vcXCuqylAAAA']。
添加用户值,现在客户列表如下:[{'wukBbRD3vcXCuqylAAAA':username}]
在此之后,我会定期循环访问客户端列表(每1秒钟左右)并为每个客户端启动一个mongodb流。然后我会使用他们的套接字ID将结果数据流式传输到该客户端。 (我假设在断开连接时,客户端密钥:val对将被删除,可能必须添加额外的代码来杀死mongodb流。)
我已经提出了下面的代码,它不能正确执行上述操作,因为我目前正试图弄清楚如何将用户名与socketio客户端ID配对。
这是构建实时仪表板的好方法吗?我越是接受这个,它看起来似乎越小/可扩展。谢谢大家的帮助!
/ bin中/万维网
var app = require('../app');
var debug = require('debug')('project:server');
var http = require('http');
//misc server initialization stuff left out to save space. this file calls in sockets.js below
var io = require('socket.io').listen(server);
require('../sockets')(io);
sockets.js
var passport = require('passport');
var cookieParser = require('cookie-parser');
var redis = require('redis').createClient();
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var socketioRedis = require("passport-socketio-redis");
var History = require('./models/history');
var clientinfo;
var user_name;
module.exports = function (io) {
io.use(socketioRedis.authorize({
passport:passport,
cookieParser: cookieParser,
key: 'express.sid',
secret: 'keyboard cat',
store: new RedisStore({ host: 'localhost', port: 6379, client: redis }),
success: authorizeSuccess, //callback on success
fail: authorizeFail //callback on fail/error
}));
function authorizeSuccess(data, accept){
//get the username
user_name = data.user.id;
accept();
}
function authorizeFail(data, message, error, accept)
{
if(error)
accept(new Error(message));
}
io.on('connection', function(socket){
//get list of clients - I believe I need to somehow add the username to the client list here.
clientinfo = Object.keys(io.engine.clients)
});
setInterval(function(){
for (key in clientdict) {
//start new stream for each client (not 100% sure multiple streams can exist)
streamData (key, clientdict[key])
}
}
},1000);
//create mongodb stream and emit to client
function streamData (u_id, sockvar) {
var stream = History.find({ user_id: u_id}, {'_id':0, 'test':1}).tailable(true).stream();
stream.on('data', function (doc) {
io.to(sockvar).emit('dash_data', { dash_data: doc });
});
}
}
答案 0 :(得分:0)
可能已经解决了这个问题 - 应该更好地阅读文档。原来,passport-socketio-redis允许您访问“socket.request.user”,它返回有关用户的信息。请参阅以下代码,了解新连接/断开连接时密钥:值对的创建和删除
io.on('connection', function(socket){
clientdict[socket.request.user.id] = socket.id;
//console.log(socket.request.user)
//all clients console.log(Object.keys(io.engine.clients))
console.log(clientdict)
socket.on('disconnect', function(){
delete clientdict[socket.request.user.id]
console.log('removed')
console.log(socket.request.user.id)
});
});