重启后无法在sailsjs中获取socket的会话 [英] Unable to get session for socket in sailsjs after restart

查看:88
本文介绍了重启后无法在sailsjs中获取socket的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在sailsjs上构建套接字驱动的应用程序,它很小,我只需要内存会话适配器,但是当我重新启动应用程序时,我得到 socket.get('/')

I trying to build socket driven application on sailsjs, it is small and I need only memory session adapter, but when I restart application, I getting after socket.get('/'):

Could not parse: No session data returned, and an error was encountered saving session data for the first time: undefined SyntaxError {stack: (...), message: "Unexpected token N"} sails.io.js:142

Uncaught Error: Server response could not be parsed!
No session data returned, and an error was encountered saving session data for the first time: undefined 

我知道我的会话丢失了,但找不到任何方法来恢复它,甚至无法处理这个错误。

I understand that my session is lost, but can't find any way to restore it, or even take care of this error.

有没有办法在不重新加载页面的情况下为套接字请求新的会话cookie?

Is there any way to request new session cookie for socket without reloading the page ?

推荐答案

这里的问题是默认情况下Sails使用 MemoryStore 来处理会话,这意味着每当你降低Sails时(或者每当应用程序崩溃时,即使它是正在重新启动永远等)所有会话将自动消失。

The problem here is that by default Sails uses MemoryStore to handle sessions, which means whenever you lower Sails (or whenever the application crashes, even if it is being restarted with forever, etc.) all the sessions will just be automatically gone.

解决方案很简单将会话处理更改为外部适配器。在默认情况下生成的 config / session.js 中,您已经有两个这样的适配器设置示例,即Redis和MongoDB,所以您需要做的就是取消注释相应的行(并且不要忘记 secret 值后的逗号)。例如,如果你使用mongo,它将类似于

The solution is as simple as changing the session handling to an external adapter. In config/session.js generated by default you already have two examples of setup for such an adapter, namely, for Redis and MongoDB, so all you have to do is to uncomment corresponding lines (and don't forget a comma after secret value). For example, if you go with mongo, it'll be something like

module.exports.session = {
  secret: 'c827f7e0dc520d83d63b5c9c055b1d00', // !!

  // Redis setup example is here...    

  // Uncomment the following lines to use your Mongo adapter as a session store
  adapter: 'mongo',
  host: 'localhost',
  port: 27017,
  db: 'sails',
  collection: 'sessions'

  // Optional Values:
  //
  // # Note: url will override other connection settings
  // url: 'mongodb://user:pass@host:port/database/collection',
  //
  // username: '',
  // password: '',
  // auto_reconnect: false,
  // ssl: false,
  // stringify: true    
};

当然,在这种情况下你应该有 mongod 在您的机器上运行。

Of course, in this case you should have mongod running on your machine.

更新

好的,如果有的话一个无法避免的MemoryStore限制(这是原始问题),然后重新初始化套接字会话而不重新加载页面变得有点棘手,但仍然可能。为了做到这一点,应该:

Ok, if there is a MemoryStore limitation which can't be avoided (that was the original question), then reinitializing the socket session without reloading the page becomes a bit tricky, but still possible. In order to do it one should:


  1. 制作一个AJAX(或CORS的JSONP)请求到服务器获取新的会话ID。例如,它可以像让控制器返回带有会话ID的JSON一样简单,假设 api / controllers / TestController.js

module.exports = {
index:function(req,res){
res.send(req.sessionID);
}
};

所以,一旦你检测到如果会话丢失,请使用 $。get('/ test')获取新的会话ID。

So, once you detected that the session is lost, use $.get('/test') to get the new session ID.


  1. 然后在重新连接套接字时将新会话ID作为 cookie 参数传递:

socket.socket.connect('/?cookie =< newSessionID>')

socket.get(/,function(response){console.log(response);})将在此之后工作。

这是一种快速破解,并且可能有更优雅的方式来包装它(例如在标题中发送会话ID,而不是在正文中发送会话ID等。),但想法很简单:发出一个AJAX请求来获取一个新会话并将其ID作为 cookie 参数传递给重新连接套接字。

It's kind of a quick hack, and there's probably a more elegant way to wrap it (e.g. send session ID in headers, not in the body, etc.), but the idea is simple: make an AJAX-request to get a new session and pass its ID as cookie parameter to reconnect the socket.

这篇关于重启后无法在sailsjs中获取socket的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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