lang/node

express-socket.io-session

C/H 2018. 2. 13. 14:53

Install

npm install express-socket.io-session

Example

var app = require('express')(),
server  = require("http").createServer(app),
io = require("socket.io")(server),
session = require("express-session")({
    secret: "my-secret",
    resave: true,
    saveUninitialized: true
}),
sharedsession = require("express-socket.io-session");


// Attach session
app.use(session);

// Share session with io sockets

io.use(sharedsession(session));

io.on("connection", function(socket) {
    // Accept a login event with user's data
    socket.on("login", function(userdata) {
        socket.handshake.session.userdata = userdata;
        socket.handshake.session.save();
    });
    socket.on("logout", function(userdata) {
        if (socket.handshake.session.userdata) {
            delete socket.handshake.session.userdata;
            socket.handshake.session.save();
        }
    });        
});

server.listen(3000);


반응형