Expressjs + socket.io +快递会话

时间:2014-07-10 14:20:38

标签: node.js express socket.io-1.0

我的server.js中有这段代码。

var app = require('express')(),
        session = require('express-session'),
        cookie = require('cookie'),
        cookieParser = require('cookie-parser'),
        manager = require('./sockets/manager');

var sessionStore = new session.MemoryStore();


app.use(cookieParser('secret'));
app.use(session({
    name: 'sid',
    store: sessionStore,
    secret: 'secret',
    saveUninitialized: true,
    resave: true,
    cookie: {
        path: '/',
        httpOnly: true,
        secure: false,
        maxAge: null
    }
}));

var server = require('http').Server(app).listen(8888),
    io = require('socket.io')(server);

io.use(function(socket, next) {
    var data= socket.request;

    if (data.headers.cookie) {
        data.cookie = cookie.parse(cookieParser.signedCookie(data.headers.cookie, 'secret'));

        console.log('data.cookies ( %s )', JSON.stringify(data.cookie)); 
        // print 'io=id_place', cookies doesn't have sid, why?

        if (data.cookie.sid) {
            data.sid = request.cookie.sid;
            sessionStore.get(request.cookie.sid, function(err, session) {
                request.session = session;
            });
        }
    }

    next();
});

manager.use(io);

控制台日志向我显示此cookie request.cookies({" io":" lHKSseNH5UrnJisSAAAA"})。 我刚刚看过很多关于expressjs 3和socket 0.9的例子,但我有:

  

" socket.io":" ^ 1.0.6",       "表达":" ~4.5.1",       " express-session":" ~1.6.4"

并且它不起作用。

我有任何错误吗?

3 个答案:

答案 0 :(得分:5)

我已经为您创建了解决方案:enter link description here。正如您所看到的,套接字对象具有握手属性,其中包含带cookie的请求标头。

答案 1 :(得分:0)

以下链接有一个非常简单的解决方案,用于在Express和Socket.IO之间共享会话。

Simple Answer

Express 4.x的中间件API非常接近Socket.IO 1.x,我们可以使用相同的会话中间件。

那边的例子使用MongoDB会话存储。我尝试使用快速会话的默认会话存储,但它运行良好。

io.js

var io = require('socket.io')();

io.on('connection', function (socket) {
    console.log(socket.request.session);
    console.log('New client connected!');
});

module.exports = io;

app.js

var express = require('express'),
    path = require('path'),
    favicon = require('serve-favicon'),
    logger = require('morgan'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    expressSession = require('express-session'),
    io = require('./io'),

    index = require('./routes/index'),

    app = express(),

    mongoose = require('mongoose'),
    uri = 'localhost:27017/selfie_showcase',
    options = {
        db: { native_parser: true },
        server: { socketOptions: { keepAlive: 1 } },
        replset: { socketOptions: { keepAlive: 1 } },
        user: 'arun',
        pass: 'arun'
    },

    sessionMiddleware = expressSession({
        secret: 'my_s3cr3t_s3ss1on',
        resave: true,
        saveUninitialized: true
    });


// connect to MongoDB
mongoose.connect(uri, options, function(err) {
    if(err) {
        console.log(err);
    }
});

// configure socket.io to share session with express
io.use(function(socket, next) {
    sessionMiddleware(socket.request, {}, next);
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(sessionMiddleware);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            title: 'Error',
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        title: 'Error',
        message: err.message,
        error: {}
    });
});


module.exports = app;

bin / www

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var io = require('../io');
var debug = require('debug')('selfie-showcase:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);
io.attach(server);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

希望这可以帮助任何未来的用户。

P.S:我使用express-generator所以这种模块分裂了。

答案 2 :(得分:0)

试试这个模块express-socket.io-session

它允许您在express和socket.io之间共享基于cookie的session数据(反之亦然)。

您可以使用所需的任何存储来存储会话数据,因为您可以以常规方式创建快速会话对象,然后与socket.io共享它。

适用于express> 4.0和socket.io> 1.0