使用快速会话的套接字IO-socket.request.res未定义 [英] Socket IO using express session - socket.request.res undefined

查看:118
本文介绍了使用快速会话的套接字IO-socket.request.res未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用express 4.x和socket io 1.4来实现会话管理,请参考

I am currently trying to implement session management using express 4.x and socket io 1.4, referencing this answer. The problem is that the second argument to the express session function is the res (response) object which is returning 'undefined' in my route. Is this question outdated or am I doing something wrong?

var http = require('http');

var session = require('express-session')(
{
  saveUninitialized : false,
  resave:false,
  secret:'secretstuff',
  cookie : {
    path : '/'
  }
}
);

var express = require('express');

var app = express();

app.use(session)

var http_server = http.createServer(app);

var io = require('./sockets')(http_server,session);

这是我的sockets.js

here is my sockets.js

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

module.exports = function(http_server, session)
{
   var io = new Server(http_server);

   io.use(function(socket,next){

      //socket.request.res === undefined
      session(socket.request, socket.request.res,next);
   })

}

这就是我得到的

未捕获到的TypeError:参数res是必需的

Uncaught TypeError: argument res is required

在许多资源中提出的

推荐答案

sessionMiddleware()函数对我来说效果很好:

sessionMiddleware() function proposed in many sources worked fine for me:

io.use(function(socket, next) {
    sessionMiddleware(socket.request, socket.request.res, next);
});

使用网络socket.io-client< => socket.io node.js服务器,直到我将socket.io-client-cpp应用程序添加到链中-服务器在socket.io-client-cpp连接上崩溃并出现上述错误:

with web socket.io-client <=> socket.io node.js server until I added socket.io-client-cpp application to the chain - server crashed on socket.io-client-cpp connection with above error:

TypeError: argument res is required

sessionMiddleware()功能中的

.人们建议从中间件中完全删除socket.request.res:

  • https://stackoverflow.com/a/29448630/630169
  • https://github.com/socketio/socket.io/issues/2971#issuecomment-326084664

,并替换为{}.但是,如果有人仍然需要并使用socket.request.res:

and replace with {}. However think it could be changed to slightly better variant in case somebody still need and use socket.request.res:

io.use(function(socket, next) {
    sessionMiddleware(socket.request, socket.request.res || {}, next);
});

这对我来说很好!

这篇关于使用快速会话的套接字IO-socket.request.res未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆