没有express.js的Socket.io会话?

Fel*_*rtz 8 session websocket node.js express socket.io

我想通过node.js和socket.io对websockets进行会话处理,而不必使用cookie并避免使用express.js,因为应该还有客户端不在浏览器环境中运行.有人已经做过这个或者有过概念验证的经验吗?

Hor*_*ein 1

这应该会有帮助,https://github.com/LearnBoost/socket.io/wiki/Authorizing

您可以跟踪所有会话变量,并使用握手数据中提供的以下内容的组合来唯一标识用户

{
   headers: req.headers       // <Object> the headers of the request
 , time: (new Date) +''       // <String> date time of the connection
 , address: socket.address()  // <Object> remoteAddress and remotePort object
 , xdomain: !!headers.origin  // <Boolean> was it a cross domain request?
 , secure: socket.secure      // <Boolean> https connection
 , issued: +date              // <Number> EPOCH of when the handshake was created
 , url: request.url          // <String> the entrance path of the request
 , query: data.query          // <Object> the result of url.parse().query or a empty object
}
Run Code Online (Sandbox Code Playgroud)

这个示例也可能有帮助,只需让您的非浏览器客户端以不同的方式提供信息即可:

SocketIO + MySQL 身份验证