index.js
2.03 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var debug = require('debug')('express-socket.io-session:example'),
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('../');
// Attach session
app.use(session);
// Share session with io sockets
io.use(
sharedsession(session, {
autoSave: true
})
);
//Debugging express
app.use('*', function(req, res, next) {
debug('Express `req.session` data is %j.', req.session);
next();
});
// Debugging io
io.use(function(socket, next) {
debug('socket.handshake session data is %j.', socket.handshake.session);
next();
});
app.use(require('express').static(__dirname));
// Set session data via express request
app.use('/login', function(req, res, next) {
debug('Requested /login');
req.session.user = {
username: 'OSK'
};
//req.session.save();
res.redirect('/');
});
// Unset session data via express request
app.use('/logout', function(req, res, next) {
debug('Requested /logout');
delete req.session.user;
//req.session.save();
res.redirect('/');
});
io.on('connection', function(socket) {
socket.emit('sessiondata', socket.handshake.session);
// Set session data via socket
debug('Emitting session data');
socket.on('login', function() {
debug('Received login message');
socket.handshake.session.user = {
username: 'OSK'
};
debug('socket.handshake session data is %j.', socket.handshake.session);
// socket.handshake.session.save();
//emit logged_in for debugging purposes of this example
socket.emit('logged_in', socket.handshake.session);
});
// Unset session data via socket
socket.on('logout', function() {
debug('Received logout message');
socket.handshake.session.user = {};
// socket.handshake.session.save();
//emit logged_out for debugging purposes of this example
debug('socket.handshake session data is %j.', socket.handshake.session);
socket.emit('logged_out', socket.handshake.session);
});
});
server.listen(3000);