Преглед на файлове

Fixed issue where users with invalid device times couldn't see songs, and fixed some other issues.

KrisVos130 преди 8 години
родител
ревизия
a92161d345
променени са 5 файла, в които са добавени 44 реда и са изтрити 13 реда
  1. 2 1
      backend/index.js
  2. 5 1
      backend/logic/actions/apis.js
  3. 5 2
      backend/logic/stations.js
  4. 3 3
      frontend/App.vue
  5. 29 6
      frontend/components/Station/Station.vue

+ 2 - 1
backend/index.js

@@ -15,7 +15,8 @@ const notifications = require('./logic/notifications');
 const config = require('config');
 
 process.on('uncaughtException', err => {
-	console.log(`ERROR: ${err}`);
+	//console.log(`ERROR: ${err.message}`);
+	console.log(`ERROR: ${err.stack}`);
 });
 
 async.waterfall([

+ 5 - 1
backend/logic/actions/apis.js

@@ -48,6 +48,10 @@ module.exports = {
 			utils.socketJoinRoom(session.socketId, `admin.${page}`);
 		}
 		cb({});
-	})
+	}),
+
+	ping: (session, cb) => {
+		cb({date: Date.now()});
+	}
 
 };

+ 5 - 2
backend/logic/stations.js

@@ -72,6 +72,7 @@ module.exports = {
 						if (station.currentSong) {
 							let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
 							if (isNaN(timeLeft)) timeLeft = -1;
+							timeLeft = Math.floor(timeLeft);
 							if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
 								this.skipStation(station._id)((err, station) => {
 									cb(err, station);
@@ -427,8 +428,10 @@ module.exports = {
 										cache.hget('sessions', session.sessionId, (err, session) => {
 											if (!err && session) {
 												db.models.user.findOne({_id: session.userId}, (err, user) => {
-													if (user.role === 'admin') socket.emit("event:station.nextSong", station._id, station.currentSong);
-													else if (station.type === "community" && station.owner === session.userId) socket.emit("event:station.nextSong", station._id, station.currentSong);
+													if (!err && user) {
+														if (user.role === 'admin') socket.emit("event:station.nextSong", station._id, station.currentSong);
+														else if (station.type === "community" && station.owner === session.userId) socket.emit("event:station.nextSong", station._id, station.currentSong);
+													}
 												});
 											}
 										});

+ 3 - 3
frontend/App.vue

@@ -1,6 +1,6 @@
 <template>
 	<div>
-		<h1 v-if="!socketConnected" class="socketNotConnected">Could not connect to the server.</h1>
+		<h1 v-if="!socketConnected" class="alert">Could not connect to the server.</h1>
 		<router-view></router-view>
 		<toast></toast>
 		<what-is-new></what-is-new>
@@ -96,7 +96,7 @@
 									date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
 									document.cookie = `SID=${result.SID}; expires=${date.toGMTString()}; domain=${cookie.domain}; secure=${cookie.secure}; path=/`;
 									location.reload();
-								}
+								});
 							} else _this.$router.go('/login');
 						}, 4000);
 					} else Toast.methods.addToast(result.message, 8000);
@@ -154,7 +154,7 @@
 		left: 0;
 	}
 
-	.socketNotConnected {
+	.alert {
 		padding: 20px;
 		color: white;
 		background-color: red;

+ 29 - 6
frontend/components/Station/Station.vue

@@ -122,7 +122,8 @@
 				station: {},
 				skipVotes: 0,
 				privatePlaylistQueueSelected: null,
-				automaticallyRequestedSongId: null
+				automaticallyRequestedSongId: null,
+				systemDifference: 0
 			}
 		},
 		methods: {
@@ -163,7 +164,7 @@
 									local.player.seekTo(local.timeBeforePause / 1000, true);
 									local.player.pauseVideo();
 								}
-								if (event.data === 2 && !local.paused && !local.noSong && (local.getTimeElapsed() / 1000) < local.currentSong.duration) {
+								if (event.data === 2 && !local.paused && !local.noSong && (local.player.getDuration() / 1000) < local.currentSong.duration) {
 									local.player.seekTo(local.getTimeElapsed() / 1000 + local.currentSong.skipDuration, true);
 									local.player.playVideo();
 								}
@@ -174,7 +175,7 @@
 			},
 			getTimeElapsed: function() {
 				let local = this;
-				if (local.currentSong) return Date.now() - local.startedAt - local.timePaused;
+				if (local.currentSong) return Date.now2() - local.startedAt - local.timePaused;
 				else return 0;
 			},
 			playVideo: function() {
@@ -199,18 +200,19 @@
 			},
 			formatTime: function(duration) {
 				let d = moment.duration(duration, 'seconds');
+				if (duration < 0) return "0:00";
 				return ((d.hours() > 0) ? (d.hours() < 10 ? ("0" + d.hours() + ":") : (d.hours() + ":")) : "") + (d.minutes() + ":") + (d.seconds() < 10 ? ("0" + d.seconds()) : d.seconds());
 			},
 			calculateTimeElapsed: function() {
 				let local = this;
-				let currentTime = Date.now();
+				let currentTime = Date.now2();
 
 				if (local.currentTime !== undefined && local.paused) {
-					local.timePaused += (Date.now() - local.currentTime);
+					local.timePaused += (Date.now2() - local.currentTime);
 					local.currentTime = undefined;
 				}
 
-				let duration = (Date.now() - local.startedAt - local.timePaused) / 1000;
+				let duration = (Date.now2() - local.startedAt - local.timePaused) / 1000;
 				let songDuration = local.currentSong.duration;
 				if (songDuration <= duration) local.player.pauseVideo();
 				if ((!local.paused) && duration <= songDuration) local.timeElapsed = local.formatTime(duration);
@@ -356,11 +358,32 @@
 					_this.socket.emit('stations.getPlaylist', _this.stationId, res => {
 				 		if (res.status == 'success') _this.songsList = res.data;
 				 	});
+					// UNIX client time before ping
+					let beforePing = Date.now();
+					_this.socket.emit('apis.ping', res => {
+						// UNIX client time after ping
+						let afterPing = Date.now();
+						// Average time in MS it took between the server responding and the client receiving
+						let connectionLatency = (afterPing - beforePing) / 2;
+						console.log(connectionLatency, beforePing - afterPing);
+						// UNIX server time
+						let serverDate = res.date;
+						// Difference between the server UNIX time and the client UNIX time after ping, with the connectionLatency added to the server UNIX time
+						let difference = (serverDate + connectionLatency) - afterPing;
+						console.log("Difference: ", difference);
+						if (difference > 3000 || difference < -3000) {
+							console.log("System time difference is bigger than 3 seconds.");
+						}
+						_this.systemDifference = difference;
+					});
 				});
 			}
 		},
 		ready: function() {
 			let _this = this;
+			Date.now2 = function() {
+				return new Date().getTime() + _this.systemDifference;
+			};
 			_this.stationId = _this.$route.params.id;
 			window.stationInterval = 0;