javascript - socket.io如何解析登录成功后的cookie(web框架用了Express)
迷茫
迷茫 2017-04-11 11:37:35
0
1
320

我已经导入了cookie-parser模块了:

let express = require('express');
let session = require('express-session');
let bodyParser = require("body-parser");
let cookieParser = require('cookie-parser');
let RedisStore = require('connect-redis')(session);
let app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let mysql = require('mysql');
    
// 设置 Cookie
app.use(cookieParser('ldjfdhslf'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));

    // 设置session
app.use(session({
    secret: 'ldjfdhslf',
    name: 'chat.id',
    store: new RedisStore({
        host: "127.0.0.1",
        port: 6379,
        db: "chat_session"
    }),
    cookie: {maxAge: 24 * 60 * 60 * 1000},
    resave: false,
    saveUninitialized: true
}));
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(1)
小葫芦
// 设置socket的session验证
io.use(function (socket, next) {
  if (socket.request.headers.cookie) {
    socket.request.cookies = cookie.parse(socket.request.headers.cookie);
    next();
  } else {
    return next(new Error('Missing cookie headers'));
  }
});

io.on('connection', function (socket) {
  console.log(socket.request.cookies);
});
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!