1

我正在使用 node.js + express + socket.io。但是当我尝试使用 cookie 时 - 我遇到了一个错误

没有 cookie 传输

我已经在这个网站上查看了所有答案。但是找不到解决办法。这是我的服务器代码:

// Require server config
var server_config = require('./config.json');

// Require express
var express = require("express");
var MemoryStore = express.session.MemoryStore;
var app = express();
var sessionStore = new MemoryStore();

// Configure app
app.configure(function () {
    app.use(express.cookieParser());
    app.use(express.session({secret: 'secret', key: 'express.sid'}));

    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname));
});

// Require socket IO and create server
var io = require('socket.io').listen(app.listen(server_config.port));

var history = {};
history.rooms = [];

var parseCookie = require('express/node_modules/cookie').parse;
io.set('authorization', function (data, accept) {
    // check if there's a cookie header
    if (data.headers.cookie) {
        // if there is, parse the cookie
        data.cookie = parseCookie(data.headers.cookie);
        // note that you will need to use the same key to grad the
        // session id, as you specified in the Express setup.
        data.sessionID = data.cookie['express.sid'];

        data.getSession = function (cb) {
            sessionStore.get(data.sessionID, function (err, session) {
                if (!err && !session) err = 'No session';
                data.session = session;
                cb(err, session);
            });
        }


    } else {
        // if there isn't, turn down the connection with a message
        // and leave the function.
        return accept('No cookie transmitted.', false);
    }
    // accept the incoming connection
    accept(null, true);
});

// On connection actions
io.sockets.on('connection', function (socket) {

    socket.handshake.getSession(function (error, session) {
        console.log(error);
    });

    // Draw action
    socket.on('drawClick', function (data) {
        // Push element to the history

        /*if (history.rooms[socket.room])
         history.rooms[socket.room].push(data);*/

        socket.broadcast.to(socket.room).emit('draw', {socket_id: socket.id, shape: data.shape, canvas_id: data.canvas_id, history: data.history});
    });

    // Subscribe to a room
    socket.on('subscribe', function (data) {

        socket.room = data.room;
        socket.join(socket.room);

        // If room history does not exists - create it

        /*if (!history.rooms[socket.room])
         history.rooms[socket.room] = [];
         // If history exists - draw it
         else
         io.sockets.socket(socket.id).emit('history', {history: history.rooms[socket.room]});*/


    });

    // Note that it is not necessary to call socket.leave() during the disconnect event.
    // This will happen automatically. Empty rooms will be automatically pruned so there is no need to manually remove them.
    socket.on('unsubscribe', function (data) {
        socket.leave(socket.room);
    });

});

这是我在客户端上的初始化方式:

io.connect(myprepa.config.site_url + ":" + myprepa.config.port);

请帮忙。

4

0 回答 0