Bläddra i källkod

Fixed issues with switching to a different station.

KrisVos130 8 år sedan
förälder
incheckning
3b16ed2b24
3 ändrade filer med 27 tillägg och 19 borttagningar
  1. 13 15
      backend/logic/utils.js
  2. 7 4
      frontend/components/Station/Station.vue
  3. 7 0
      frontend/main.js

+ 13 - 15
backend/logic/utils.js

@@ -163,24 +163,24 @@ module.exports = {
 	socketLeaveRooms: function(socketid) {
 		let socket = this.socketFromSession(socketid);
 		let rooms = socket.rooms;
-		for (let j = 0; j < rooms.length; j++) {
-			socket.leave(rooms[j]);
+		for (let room in rooms) {
+			socket.leave(room);
 		}
 	},
 	socketJoinRoom: function(socketId, room) {
 		let socket = this.socketFromSession(socketId);
 		let rooms = socket.rooms;
-		for (let j = 0; j < rooms.length; j++) {
-			socket.leave(rooms[j]);
+		for (let room in rooms) {
+			socket.leave(room);
 		}
 		socket.join(room);
 	},
 	socketJoinSongRoom: function(socketId, room) {
 		let socket = this.socketFromSession(socketId);
 		let rooms = socket.rooms;
-		for (let j = 0; j < rooms.length; j++) {
-			if (socket.indexOf('song.') !== -1) {
-				socket.leave(rooms[j]);
+		for (let room in rooms) {
+			if (room.indexOf('song.') !== -1) {
+				socket.leave(rooms);
 			}
 		}
 		socket.join(room);
@@ -189,10 +189,9 @@ module.exports = {
 		for (let id in sockets) {
 			let socket = sockets[id];
 			let rooms = socket.rooms;
-			for (let roomId in rooms) {
-				console.log(roomId);
-				if (roomId.indexOf('song.') !== -1) {
-					socket.leave(roomId);
+			for (let room in rooms) {
+				if (room.indexOf('song.') !== -1) {
+					socket.leave(room);
 				}
 			}
 			socket.join(room);
@@ -202,10 +201,9 @@ module.exports = {
 		for (let id in sockets) {
 			let socket = sockets[id];
 			let rooms = socket.rooms;
-			for (let roomId in rooms) {
-				console.log(roomId);
-				if (roomId.indexOf('song.') !== -1) {
-					socket.leave(roomId);
+			for (let room in rooms) {
+				if (room.indexOf('song.') !== -1) {
+					socket.leave(room);
 				}
 			}
 		}

+ 7 - 4
frontend/components/Station/Station.vue

@@ -82,7 +82,6 @@
 				timePaused: 0,
 				paused: false,
 				timeElapsed: "0:00",
-				interval: 0,
 				liked: false,
 				disliked: false,
 				modals: {
@@ -166,8 +165,8 @@
 					local.player.loadVideoById(local.currentSong._id);
 
 					if (local.currentSong.artists) local.currentSong.artists = local.currentSong.artists.join(", ");
-					if (local.interval !== 0) clearInterval(local.interval);
-					local.interval = setInterval(function () {
+					if (window.stationInterval !== 0) clearInterval(window.stationInterval);
+					window.stationInterval = setInterval(function () {
 						local.resizeSeekerbar();
 						local.calculateTimeElapsed();
 					}, 250);
@@ -285,11 +284,12 @@
 		ready: function() {
 			let _this = this;
 			_this.stationId = _this.$route.params.id;
-			_this.interval = 0;
+			window.stationInterval = 0;
 
 			let socketInterval = setInterval(() => {
 				if (!!_this.$parent.socket) {
 					_this.socket = _this.$parent.socket;
+					_this.socket.removeAllListeners();
 					_this.socket.emit('stations.join', _this.stationId, res => {
 						if (res.status === "success") {
 							_this.currentSong = (res.data.currentSong) ? res.data.currentSong : {};
@@ -300,6 +300,7 @@
 							if (res.data.currentSong) {
 								_this.noSong = false;
 								_this.simpleSong = (res.data.currentSong.likes === -1 && res.data.currentSong.dislikes === -1);
+								console.log(12334);
 								_this.youtubeReady();
 								_this.playVideo();
 								_this.socket.emit('songs.getOwnSongRatings', res.data.currentSong._id, data => {
@@ -325,6 +326,7 @@
 							//TODO Handle error
 						}
 					});
+
 					_this.socket.on('event:songs.next', data => {
 						_this.currentSong = (data.currentSong) ? data.currentSong : {};
 						_this.startedAt = data.startedAt;
@@ -333,6 +335,7 @@
 						if (data.currentSong) {
 							_this.noSong = false;
 							_this.simpleSong = (data.currentSong.likes === -1 && data.currentSong.dislikes === -1);
+							console.log(1233, _this.stationId);
 							if (!_this.playerReady) _this.youtubeReady();
 							else _this.playVideo();
 							_this.socket.emit('songs.getOwnSongRatings', data.currentSong._id, (data) => {

+ 7 - 0
frontend/main.js

@@ -26,6 +26,13 @@ lofig.get('serverDomain', function(res) {
 });
 
 router.beforeEach(transition => {
+	if (window.stationInterval) {
+		clearInterval(window.stationInterval);
+		window.stationInterval = 0;
+	}
+	if (window.socket) {
+		window.socket.removeAllListeners();
+	}
 	if (transition.to.loginRequired || transition.to.adminRequired) {
 		auth.getStatus((authenticated, role) => {
 			if (transition.to.loginRequired && !authenticated) transition.redirect('/login');