-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.js
159 lines (129 loc) · 4.31 KB
/
server.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var express = require('express');
var bodyParser = require('body-parser');
var mongo = require('mongodb').MongoClient;
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var geoip = require('geoip-lite');
var db;
var playlistController = {};
var playlistClients = {};
var latestVersion = {};
app.set('port', process.env.PORT || 8080);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/dist', express.static(__dirname + '/dist'));
app.use('/components', express.static(__dirname + '/components'));
// development only
// if ('development' == app.get('env')) {
// console.log('Development mode.');
// app.use(function(req, res, next){
// //console.log('%s %s', req.method, req.url);
// next();
// });
// }
app.post('/p', function(req, res){
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if(req.body.id){
// update - just overwrite for now
playlistController.update(req.body.id, req.body, res.send.bind(res));
} else {
// insert new record
var data = req.body;
data.ip = ip;
playlistController.insert(data, res.send.bind(res));
}
});
app.get('/p', function(req, res){
playlistController.get(req.query.id, res.send.bind(res));
});
app.get('*', function(req, res){
res.sendFile(__dirname + "/index.html");
});
// db - Mongo Connect
mongo.connect("mongodb://localhost/greatdj", function(err, database) {
db = database;
http.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
});
// socket io for realtime updates on playlists across clients
io.on('connection', function(socket){
// socket is the client socket
// global vars that belong to each socket
var plId;
console.log(' * new connection');
socket.on('register', function(data){
console.log(' * client connected', data.id);
playlistClients[data.id] = playlistClients[data.id] || [];
playlistClients[data.id].push(socket);
plId = data.id;
if(playlistClients[data.id].length > 1 && latestVersion[data.id]){
socket.emit('playlistChange', latestVersion[data.id]);
} else {
playlistController.get(data.id, function(data){
socket.emit('playlistChange', data);
});
}
});
socket.on('disconnect', function(){
console.log(' * client disconnected', plId);
//remove from playlistClients playlistClients
if(plId){
for (var i = playlistClients[plId].length - 1; i >= 0; i--) {
if(playlistClients[plId][i] === socket){
playlistClients[plId].splice(i, 1);
return;
}
}
if(!playlistClients[plId].length){
latestVersion[plId] = null;
}
plId = null;
}
});
socket.on('unregister', function(){
console.log(' * client unregister', plId);
//remove from playlistClients playlistClients
if(plId){
for (var i = playlistClients[plId].length - 1; i >= 0; i--) {
if(playlistClients[plId][i] === socket){
playlistClients[plId].splice(i, 1);
return;
}
}
plId = null;
}
});
socket.on('changedPlaylist', function(data){
console.log(' * changedPlaylist:', data.id);
latestVersion[data.id] = data;
for (var i = playlistClients[data.id].length - 1; i >= 0; i--) {
var subscriber = playlistClients[data.id][i];
if(subscriber !== socket){
subscriber.emit('playlistChange', data);
}
}
});
});
// playlist controllers controllers - get them out of here some day
playlistController.insert = function(data, callback){
var id = Math.random().toString(36).slice(3,9); // revisit
var geo = geoip.lookup(data.ip);
var doc = {id: id, playlist: data.playlist, ip: data.ip, geo: geo, created: new Date()};
db.collection('playlists').insert(doc, {w:1}, function(err, result) {
console.log('insert ok ', id);
callback({operation: 'insert', id: id});
});
};
playlistController.update = function(id, data, callback){
db.collection('playlists').update({id: id}, {$set:{playlist: data.playlist}}, {w:1}, function(err, result) {
console.log('update ok ', id);
callback({operation: 'update', id: id});
});
};
playlistController.get = function(id, callback){
db.collection('playlists').findOne({id: id}, function(err, item) {
callback(item);
});
};