«   2018/05   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Archives
Today
131
Total
1,103,078
관리 메뉴

Blue Breeze

express-socket.io-session 본문

Language/Node.js

express-socket.io-session

푸른바람 푸른_바람 2018.02.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);


'Language > Node.js' 카테고리의 다른 글

node.js Common System Errors  (0) 2018.02.23
express-socket.io-session  (0) 2018.02.13
node.js 64bit int buffer  (0) 2018.01.25
2진수 채우기 zerofill  (0) 2018.01.12
node.js Buffer  (0) 2018.01.11
node.js zlib compress uncompress  (0) 2018.01.10
0 Comments
댓글쓰기 폼