我的握手无法使socket.io工作

时间:2014-02-03 00:55:51

标签: node.js sockets handshake

我不明白我的问题在哪里。 当我说出来的时候:

io = require('socket.io').listen(server).of("/");

我的握手看起来运行正常,但我所有的socket.io事件(如连接)都无法正常工作。 而当我把它放在这个

io = require('socket.io').listen(server);

我明白了:

  

TypeError:对象#没有方法'授权'

如果我取消握手,所有socket.io都可以,但我需要它:/

这是我的app.js

 const express = require('express')
    , app  = express()
    , http = require('http')
    , ejs = require('ejs')
    , server = http.createServer(app)
    , port = process.env.PORT || 1337
    , io = require('socket.io').listen(server).of("/")
    ;



module.exports = { app: app, server: server };
// Grab "cookie" and "connect" from express
var connect = require('express/node_modules/connect')
var cookie = require('express/node_modules/cookie')
var connectes =[];


app.configure(function(){
    this.use("/public", express.static(__dirname + '/public'));
    this.use(express.cookieParser());
    this.use(express.json());
    this.use(express.urlencoded());
    this.sessionStore = new express.session.MemoryStore({ reapInterval: 60000 * 10 });
    this.use(express.session({
        "secret": "some private string",
        "store":  this.sessionStore                                            
    }));
});
app.engine('html', ejs.renderFile);

app.set('views', __dirname + '/views');

app.get('/', [requireLogin], function(request, response) {
    var pseudo = request.session.pseudo;
    response.render('index.ejs.html',{connectes:connectes});
});

app.get('/login', function(request, response) {
    response.render('login.ejs.html');
});

app.post("/login", function(req, res){
    if (req.param("user") != "") {
        req.session.pseudo = req.param("user");
        //socket.set('pseudo',req.param("user"));
        connectes.push(req.param("user"));
        res.render('index.ejs.html',{connectes:connectes});
    }else{
        res.render('login.ejs.html');
    }
});


const parseCookie = require('connect').utils.parseCookie;
io.authorization(function (handshakeData, callback) {
    var cookies = cookie.parse(handshakeData.headers.cookie);
    console.log(cookie);
});

io.on('connection', function(socket){
    console.log("This never appear in log :/");
    socket.on('login',function(user,callback){
        var pseudoValide = true;
        for (var i = 0; i < connectes.length; i++) {
            if(connectes[i]==user.pseudo){
                pseudoValide = false;
                callback(true);
            }
        }
        if (pseudoValide) {
            socket.set('pseudo',user.pseudo);
            pseudo = user.pseudo;
            socket.broadcast.emit('newuser', pseudo);
            socket.broadcast.emit('nvluseronline', pseudo);
            connectes.push(pseudo);
            callback(false,pseudo);
        }
    });
    socket.on('message',function(message){
        messageUser = message.message;
        socket.get('pseudo',function(error, pseudo){
            io.sockets.emit('nouveauMessage',{pseudo:pseudo,message:messageUser});
        })
    });
    socket.on('disconnect',function(){
        socket.get('pseudo',function(error, pseudo){
            connectes = unset(connectes, pseudo);
            io.sockets.emit('nvldisc', pseudo);
        })

    });
});

function unset(array, value){
    var output=[];
    var index = array.indexOf(value)
    {
        var j = 0;
        for(var i in array)
        {
            if (i!=index)
            {
                output[j]=array[i];
                j++;
            }
        }
        return output;
    }
}

/** Middleware  */
function requireLogin (req, res, next) {
    if (req.session.pseudo) {
        console.log(req.session.pseudo);
        next();
    }else{
        res.redirect("/login");
    }
}


if (!module.parent) {
  server.listen(port, function () {
    console.log('Listening', this.address());
  })
}

就像你可以看到我是一个初学者迷路了!

谢谢你们!

1 个答案:

答案 0 :(得分:0)

您应该仔细阅读授权documentationauthorization方法仅存在于名称空间中。在进行全局授权时,您必须执行以下操作:

io.configure(function (){
  io.set('authorization', function (handshakeData, callback) {
    callback(null, true); // error first callback style 
  });
});

如果您将.of("/")作为服务器端点,它将正常工作,但它是一个命名空间。如果没有命名空间,则必须使用上述方法。