【问题标题】:nodejs, socket.io: how to get request and response from socket function?nodejs,socket.io:如何从套接字函数获取请求和响应?
【发布时间】:2012-11-16 21:45:36
【问题描述】:

我正在创建聊天应用程序,使用 nodejs (0.8.15)、express (>3.0) 框架和 mongodb 注册用户。

var express = require('express')
  , http = require('http')
  , path = require('path')
  , io = require('socket.io');

var app = express()
  , server = http.createServer(app)
  , io = io.listen(server);

    app.configure(function() {
      app.set('port', process.env.PORT || 3000);
      app.set('views', __dirname + '/views');
      app.set('view engine', 'ejs');
      app.use(express.favicon());
      app.use(express.logger('dev'));
      app.use(express.bodyParser());
      app.use(express.methodOverride());
      app.use(express.cookieParser('secret'));
      app.use(express.session({cookie: {maxAge: 60000*100}}));
      app.use(app.router);
      app.use(express.static(path.join(__dirname, 'public')));
    });

    app.configure('development', function() {
      app.use(express.errorHandler());
    });

    app.get('/chat', function(req, res) {
        res.render('chat');
    });

 server.listen(app.get('port'), function() {
    console.log("Express server listening on port " + app.get('port'));
  });

 io.sockets.on('connection', function (socket) {
    socket.on('start-chat', function() {
         // here i need to know req and res
         // for example, i need to write:
         // socket.username = req.session.username;
    });
 });

问:在上面的代码中聊天时,如何让 res 和 req 对象与它们一起使用?还是我与用户身份验证创建聊天的方式有误?

谢谢!

已编辑:答案就在这里 http://www.danielbaulig.de/socket-ioexpress/

【问题讨论】:

    标签: node.js express socket.io


    【解决方案1】:

    您无法在 socket.io 处理程序中获取 res 和 req 对象,因为它们根本不存在 - socket.io 不是正常的 http。

    相反,您可以对用户进行身份验证并为其分配一个会话身份验证令牌(用于标识他们已登录以及他们是谁的密钥)。 然后客户端可以将身份验证令牌与每个 socket.io 消息一起发送,服务器端处理程序只需检查数据库中密钥的有效性:

    io.sockets.on('connection', function (socket) {
    socket.on('start-chat', function(message) {
         if (message.auth_token)
             //Verify the auth_token with the database of your choice here!
         else
             //Return an error message "Not Authenticated"
    });
    

    【讨论】:

      【解决方案2】:

      您需要使用authorization

      var socketIO = require('socket.io').listen(port);
      socketIO.set('authorization', function(handshakeData, cb) {
         //use handshakeData to authorize this connection
         //Node.js style "cb". ie: if auth is not successful, then cb('Not Successful');
         //else cb(null, true); //2nd param "true" matters, i guess!!
      });
      
      socketIO.on('connection', function (socket) {
         //do your usual stuff here
      });
      

      【讨论】:

        【解决方案3】:

        socket.io v1.0 及以上您可以像这样获得req 对象

        var req = socket.request;
        var res = req.res;
        

        【讨论】:

        • 这是错误的。这与所提出的请求不同。你不会在上面的 socket.request 中找到你的请求参数。
        猜你喜欢
        • 2014-10-18
        • 1970-01-01
        • 2017-11-30
        • 2013-08-09
        • 1970-01-01
        • 2019-05-24
        • 2019-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多