-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-v2.js
84 lines (73 loc) · 2.14 KB
/
socket-v2.js
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
80
81
82
83
84
var io = require('socket.io'),
express = require('express'),
connect = require('connect'),
MemoryStore = connect.session.MemoryStore;
app = express.createServer(),
sessionStore = new MemoryStore(),
Session = require('connect').middleware.session.Session,
parseCookie = require('connect').utils.parseCookie,
auth = require('./server/auth-v2'),
tio = require('./server/thoughtio-v3.js');
app.configure(function () {
app.use(connect.static(__dirname + '/public'));
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ store: sessionStore,
secret: 'this is secret',
key: 'express.sid'}));
app.use(app.router);
app.all(function (req, res, next) {
console.log('all');
next();
});
});
app.get('/', function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain',
});
res.end('<h2>Hello, use socketio to connect to me!</h2>');
});
app.post('/login', auth.post);
if (process.env.PORT)
app.listen(process.env.PORT);
else
app.listen(8888);
var sio = io.listen(app);
sio.set('authorization', function (data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['express.sid'];
data.sessionStore = sessionStore;
sessionStore.get(data.sessionID, function (err, session) {
if (err || !session) {
accept('unauthorized', false);
}
else {
data.session = new Session(data, session);
if (session.currentUser && session.auth)
accept(null, true);
else
accept('unauthorized', false);
}
});
}
else {
return accept('No cookie transmitted', false);
}
});
sio.of('/thoughts').on('connection', function (socket) {
var hs = socket.handshake;
console.log('A socket opened by user: ' + hs.session.currentUser.username
+ ' connected');
var intervalID = setInterval(function () {
hs.session.reload( function () {
hs.session.touch().save();
});
}, 60 * 1000);
tio.setup(socket);
socket.on('disconnect', function () {
console.log('A socket with sessionID ' + hs.sessionID + ' disconnected!');
clearInterval(intervalID);
});
});