Pārlūkot izejas kodu

Hopefully fixed issues where socket.io's to function doesn't work.

KrisVos130 8 gadi atpakaļ
vecāks
revīzija
d9dfd1dfd5

+ 3 - 3
backend/logic/actions/queueSongs.js

@@ -10,16 +10,16 @@ const request = require('request');
 const hooks = require('./hooks');
 
 notifications.subscribe('queue.newSong', songId => {
-	io.to('admin.queue').emit('event:song.new', { songId });
+	utils.emitToRoom('admin.queue', 'event:song.new', { songId });
 });
 
 notifications.subscribe('queue.removedSong', songId => {
-	io.to('admin.queue').emit('event:song.removed', { songId });
+	utils.emitToRoom('admin.queue', 'event:song.removed', { songId });
 });
 
 notifications.subscribe('queue.updatedSong', songId => {
 	//TODO Retrieve new Song object
-	io.to('admin.queue').emit('event:song.updated', { songId });
+	utils.emitToRoom('admin.queue', 'event:song.updated', { songId });
 });
 
 module.exports = {

+ 4 - 4
backend/logic/actions/songs.js

@@ -9,7 +9,7 @@ const hooks = require('./hooks');
 const queueSongs = require('./queueSongs');
 
 cache.sub('song.like', (data) => {
-	io.io.to(`song.${data.songId}`).emit('event:song.like', {songId: data.songId, undisliked: data.undisliked});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, undisliked: data.undisliked});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
@@ -18,7 +18,7 @@ cache.sub('song.like', (data) => {
 });
 
 cache.sub('song.dislike', (data) => {
-	io.io.to(`song.${data.songId}`).emit('event:song.dislike', {songId: data.songId, unliked: data.unliked});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.dislike', {songId: data.songId, unliked: data.unliked});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: true});
@@ -27,7 +27,7 @@ cache.sub('song.dislike', (data) => {
 });
 
 cache.sub('song.unlike', (data) => {
-	io.io.to(`song.${data.songId}`).emit('event:song.unlike', {songId: data.songId});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.unlike', {songId: data.songId});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
@@ -36,7 +36,7 @@ cache.sub('song.unlike', (data) => {
 });
 
 cache.sub('song.undislike', (data) => {
-	io.io.to(`song.${data.songId}`).emit('event:song.undislike', {songId: data.songId});
+	utils.emitToRoom(`song.${data.songId}`, 'event:song.undislike', {songId: data.songId});
 	utils.socketsFromUser(data.userId, (sockets) => {
 		sockets.forEach((socket) => {
 			socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});

+ 6 - 6
backend/logic/actions/stations.js

@@ -14,25 +14,25 @@ const songs = require('../songs');
 const hooks = require('./hooks');
 
 cache.sub('station.pause', stationId => {
-	io.io.to(`station.${stationId}`).emit("event:stations.pause");
+	utils.emitToRoom(`station.${stationId}`, "event:stations.pause");
 });
 
 cache.sub('station.resume', stationId => {
 	stations.getStation(stationId, (err, station) => {
-		io.io.to(`station.${stationId}`).emit("event:stations.resume", {timePaused: station.timePaused});
+		utils.emitToRoom(`station.${stationId}`, "event:stations.resume", {timePaused: station.timePaused});
 	});
 });
 
 cache.sub('station.queueUpdate', stationId => {
 	stations.getStation(stationId, (err, station) => {
 		if (!err) {
-			io.io.to(`station.${stationId}`).emit("event:queue.update", station.queue);
+			utils.emitToRoom(`station.${stationId}`, "event:queue.update", station.queue);
 		}
 	});
 });
 
 cache.sub('station.voteSkipSong', stationId => {
-	io.io.to(`station.${stationId}`).emit("event:song.voteSkipSong");
+	utils.emitToRoom(`station.${stationId}`, "event:song.voteSkipSong");
 });
 
 cache.sub('station.create', stationId => {
@@ -42,9 +42,9 @@ cache.sub('station.create', stationId => {
 
 		// TODO If community, check if on whitelist
 		console.log("*************", station.privacy);
-		if (station.privacy === 'public') io.io.to('home').emit("event:stations.created", station);
+		if (station.privacy === 'public') utils.emitToRoom('home', "event:stations.created", station);
 		else {
-			let sockets = io.io.to('home').sockets;
+			let sockets = utils.getRoomSockets('home');
 			console.log("*************", sockets.length);
 			for (let socketId in sockets) {
 				let socket = sockets[socketId];

+ 3 - 3
backend/logic/stations.js

@@ -389,21 +389,21 @@ module.exports = {
 							if (station.currentSong !== null && station.currentSong._id !== undefined) {
 								station.currentSong.skipVotes = 0;
 							}
-							io.io.to(`station.${station._id}`).emit("event:songs.next", {
+							utils.emitToRoom(`station.${station._id}`, "event:songs.next", {
 								currentSong: station.currentSong,
 								startedAt: station.startedAt,
 								paused: station.paused,
 								timePaused: 0
 							});
 							if (station.currentSong !== null && station.currentSong._id !== undefined) {
-								utils.socketsJoinSongRoom(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
+								utils.socketsJoinSongRoom(utils.getRoomSockets(`station.${station._id}`), `song.${station.currentSong._id}`);
 								console.log("NEXT SONG!!!", station.currentSong);
 								if (!station.paused) {
 									notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
 								}
 							} else {
 								console.log("22", !!(station.currentSong));
-								utils.socketsLeaveSongRooms(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
+								utils.socketsLeaveSongRooms(utils.getRoomSockets(`station.${station._id}`), `song.${station.currentSong._id}`);
 							}
 							cb(null, station);
 						} else cb(err);

+ 24 - 0
backend/logic/utils.js

@@ -208,6 +208,30 @@ module.exports = {
 			}
 		}
 	},
+	emitToRoom: function(room) {
+		let sockets = io.io.sockets.sockets;
+		for (let id in sockets) {
+			let socket = sockets[id];
+			if (socket.rooms[room]) {
+				let args = [];
+				for (let i = 1; i < Object.keys(arguments).length; i++) {
+					args.push(arguments[i]);
+				}
+				socket.emit.apply(socket, args);
+			}
+		}
+	},
+	getRoomSockets: function(room) {
+		let sockets = io.io.sockets.sockets;
+		let roomSockets = [];
+		for (let id in sockets) {
+			let socket = sockets[id];
+			if (socket.rooms[room]) {
+				roomSockets.push(socket);
+			}
+		}
+		return roomSockets;
+	},
 	getSongFromYouTube: (songId, cb) => {
 		const youtubeParams = [
 			'part=snippet,contentDetails,statistics,status',