|
@@ -10,6 +10,8 @@ const cache = require('../cache');
|
|
|
const notifications = require('../notifications');
|
|
|
const utils = require('../utils');
|
|
|
|
|
|
+let stationsLoaded = {};
|
|
|
+
|
|
|
/**
|
|
|
* Loads a station into the cache, and sets up all the related logic
|
|
|
*
|
|
@@ -17,7 +19,6 @@ const utils = require('../utils');
|
|
|
* @param {Function} cb - gets called when this function completes
|
|
|
*/
|
|
|
function initializeAndReturnStation (stationId, cb) {
|
|
|
-
|
|
|
async.waterfall([
|
|
|
|
|
|
// first check the cache for the station
|
|
@@ -37,30 +38,42 @@ function initializeAndReturnStation (stationId, cb) {
|
|
|
}
|
|
|
|
|
|
], (err, station) => {
|
|
|
-
|
|
|
if (err && err !== true) return cb(err);
|
|
|
|
|
|
// get notified when the next song for this station should play, so that we can notify our sockets
|
|
|
- let notification = notifications.subscribe(`stations.nextSong?id=${station.id}`, () => {
|
|
|
- // get the station from the cache
|
|
|
- cache.hget('stations', station.id, (err, station) => {
|
|
|
- if (station) {
|
|
|
- // notify all the sockets on this station to go to the next song
|
|
|
- io.to(`station.${stationId}`).emit("event:songs.next", {
|
|
|
- currentSong: station.currentSong,
|
|
|
- startedAt: station.startedAt,
|
|
|
- paused: station.paused,
|
|
|
- timePaused: 0
|
|
|
- });
|
|
|
- // schedule a notification to be dispatched when the next song ends
|
|
|
- notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
|
|
|
- }
|
|
|
- // the station doesn't exist anymore, unsubscribe from it
|
|
|
- else {
|
|
|
- notifications.remove(notification);
|
|
|
- }
|
|
|
- });
|
|
|
- }, true);
|
|
|
+ if (stationsLoaded[stationId] === undefined) {
|
|
|
+ stationsLoaded[stationId] = 1;
|
|
|
+ let notification = notifications.subscribe(`stations.nextSong?id=${station.id}`, () => {
|
|
|
+ // get the station from the cache
|
|
|
+ cache.hget('stations', station.name, (err, station) => {
|
|
|
+ if (station) {
|
|
|
+ console.log(777);
|
|
|
+ // notify all the sockets on this station to go to the next song
|
|
|
+ io.to(`station.${stationId}`).emit("event:songs.next", {
|
|
|
+ currentSong: station.currentSong,
|
|
|
+ startedAt: station.startedAt,
|
|
|
+ paused: station.paused,
|
|
|
+ timePaused: 0
|
|
|
+ });
|
|
|
+ // schedule a notification to be dispatched when the next song ends
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
|
|
|
+ }
|
|
|
+ // the station doesn't exist anymore, unsubscribe from it
|
|
|
+ else {
|
|
|
+ console.log(888);
|
|
|
+ notifications.remove(notification);
|
|
|
+ delete stationsLoaded[stationId];
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }, true);
|
|
|
+
|
|
|
+ if (!station.paused) {
|
|
|
+ console.log(station);
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return cb(null, station);
|
|
|
|
|
|
// will need to be added once station namespace thing is decided
|
|
|
// function generatePlaylist(arr) {
|
|
@@ -120,8 +133,6 @@ module.exports = {
|
|
|
* @return {{ status: String, userCount: Integer }}
|
|
|
*/
|
|
|
join: (session, stationId, cb) => {
|
|
|
- io.io.to("SomeRoom").emit("SomeRoomMessage");
|
|
|
- io.io.emit("SomeRoomMessage");
|
|
|
initializeAndReturnStation(stationId, (err, station) => {
|
|
|
|
|
|
if (err && err !== true) {
|
|
@@ -132,13 +143,13 @@ module.exports = {
|
|
|
|
|
|
if (session) session.stationId = stationId;
|
|
|
|
|
|
- //TODO Loop through all sockets, see if socket with same sessionid exists, and if so leave all other station rooms and join this stationRoom
|
|
|
+ //TODO Loop through all sockets, see if socket with same session exists, and if so leave all other station rooms and join this stationRoom
|
|
|
|
|
|
cache.client.hincrby('station.userCounts', stationId, 1, (err, userCount) => {
|
|
|
if (err) return cb({ status: 'error', message: 'An error occurred while joining the station' });
|
|
|
- utils.socketJoinRoom(sessionId);
|
|
|
+ utils.socketJoinRoom(session);
|
|
|
//TODO Emit to cache, listen on cache
|
|
|
- cb({ status: 'success', userCount });
|
|
|
+ cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
|
|
|
});
|
|
|
}
|
|
|
else {
|
|
@@ -202,7 +213,7 @@ module.exports = {
|
|
|
else if (station) {
|
|
|
cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
|
|
|
if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
|
|
|
- utils.socketLeaveRooms(sessionId);
|
|
|
+ utils.socketLeaveRooms(session);
|
|
|
cb({ status: 'success', userCount });
|
|
|
});
|
|
|
} else {
|