-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.js
More file actions
56 lines (46 loc) · 1.58 KB
/
websocket.js
File metadata and controls
56 lines (46 loc) · 1.58 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
/*!
* websocket.js
*
* @author pfleidi
*/
var Fs = require('fs');
var parseCookie = require('./utils').parseCookie;
var Session = require('connect').middleware.session.Session;
exports.handleSockets = function (io, log, store) {
io.on('connection', function (socket) {
var handshake = socket.handshake;
var session = handshake.session;
console.log('A socket with sessionID ' + handshake.sessionID + ' connected!');
var intervalID = setInterval(function () {
if (session) { session.reload(function () { session.touch().save(); }); }
}, 30 * 1000);
socket.on('disconnect', function () {
console.log('A socket with sessionID ' + handshake.sessionID + ' disconnected!');
clearInterval(intervalID);
});
socket.on('slide.change', function (data) {
if (session && session.is_admin) {
socket.broadcast.emit('slide.change', data);
}
});
});
// Autorize WebSocket
io.set('authorization', function (data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['slides.uid'];
data.sessionStore = store;
store.get(data.sessionID, function (err, session) {
if (err || !session) {
accept('Error', false);
} else {
data.session = new Session(data, session);
accept(null, true);
}
});
} else {
return accept('No cookie transmitted.', false);
}
accept(null, true);
});
};