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


반응형

'lang > node' 카테고리의 다른 글

Node.js helloWorld  (0) 2018.06.10
node.js Common System Errors  (0) 2018.02.23
node.js 64bit int buffer  (0) 2018.01.25
2진수 채우기 zerofill  (0) 2018.01.12
node.js Buffer  (0) 2018.01.11