-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.js
81 lines (64 loc) · 2.08 KB
/
sockets.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
/*
* Module dependencies
*/
var parent = module.parent.exports
, app = parent.app
, server = parent.server
, express = require('express')
, sessionStore = parent.sessionStore
, sio = require('socket.io')
, parseCookies = require('connect').utils.parseSignedCookies
, cookie = require('cookie')
, config = require('./config.json')
, utils = require('./utils')
, User = require('./providers/user').User
, Station = require('./providers/station').Station
, Tone = require('./providers/tone').Tone;
var io = sio.listen(server);
io.set('authorization', function (hsData, accept) {
console.log(hsData.headers.cookie);
if(hsData.headers.cookie) {
var cookies = parseCookies(cookie.parse(hsData.headers.cookie), config.session.secret)
, sid = cookies['tones'];
sessionStore.load(sid, function(err, session) {
if(err || !session) {
return accept('Error retrieving session!', false);
}
hsData.tones = {
user: session.passport.user._doc,
station: /\/(?:([^\/]+?))\/?$/g.exec(hsData.headers.referer)[1]
};
return accept(null, true);
});
} else {
return accept('No cookie transmitted.', false);
}
});
io.sockets.on('connection', function (socket) {
var hs = socket.handshake
, user = hs.tones.user
, station_id = hs.tones.station
, now = new Date()
socket.join(station_id);
utils.enterStation(user._id, station_id, function(){
console.log('user '+user.username+' enter in room:'+station_id);
});
socket.on('init', function(data) {
console.log('welcome to '+user.username+' room:'+station_id);
});
socket.on('addItem', function(data) {
utils.createTone(data, station_id, user._id, function(){
console.log('tone '+data.title+' has been added in station '+station_id);
})
});
socket.on('playerStopped', function (idItem) {
});
/**
* when a user disconnect
*/
socket.on('disconnect', function () {
utils.leaveStation(user._id, station_id, function(){
console.log('user '+user.username+' leave the room:'+station_id);
});
});
});