无法用我的握手 socket.io 作品



我不明白我的问题在哪里。当我放这个:

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

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

谢谢你们!

您应该仔细阅读有关授权的文档。authorization方法仅适用于名称空间。在进行全球授权时,您必须执行:

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

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

相关内容

  • 没有找到相关文章

最新更新