在我的快递路线中,我这样做
var cookie = require('cookie');
var server = require("http").Server(express);
var io = require("socket.io")(server);
server.listen(5000);
io.on('connection', function(client) {
console.log('socket connected') // returned socket connected
if(client.request.headers.cookie){
console.log(userId) //returned userId
var userId = cookie.parse(client.request.headers.cookie).userId;
client.on('call_'+userId, function(data) {
io.emit('call_'+userId,data);
});
}
});
router.use(function(req, res, next){
if(!req.user){
res.redirect('/login');
}else{
res.locals.username = req.user.username;
//set cookie for socket.io purposes
if(!cookie.parse(req.headers.cookie).userId){
res.cookie('userId',querystring.escape(req.user._id));
}
return next();
}
});
router.get('/', function(req, res, next) {
res.redirect('./dashboard');
});
我尝试对端口5000进行发射,它有效。但它只有在我登录后刷新页面才有效,当我第一次来到仪表板的页面时它不起作用。我试图在登录时查看套接字是否已连接,我确实看到它已连接。
为什么它只在我刷新后?有什么问题?