简体   繁体   中英

nodejs, socket.io: how to get request and response from socket function?

im creating chat application, using nodejs (0.8.15), express (>3.0) framework and mongodb for register users.

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;
    });
 });

Q: How to get res and req objects to work with them when chatting like on code above? or am i going wrong way of creating chat with user auth?

thank you!

EDITED: answer is here http://www.danielbaulig.de/socket-ioexpress/

You cannot get res and req objects in a socket.io handler, as they simply do not exist - socket.io is not normal http.

Instead, what you can do is authenticate users and assign them a session auth token (a key that identifies that they're logged in and who they are). Then the client can send the auth token along with every socket.io message, and the server-side handler can just check the validity of the key in the database:

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"
});

You need to use 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
});

on socket.io v1.0 and above you can get the req object like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM