Browse Source

Changed back to using namespaces due to annoying bugs.

BuildTools 8 years ago
parent
commit
0c93a55922

+ 4 - 2
backend/logic/socketHandler.js

@@ -22,11 +22,13 @@ module.exports = (core, io) => {
 			});
 		});
 
-		socket.on('/station/:id/join', (id, cb) => {
+		/*socket.on('/station/:id/join', (id, cb) => {
+			console.log("JOINED!!!");
 			core['/station/:id/join'](id, socket.id, result => {
+				console.log("CALLBACK!!!");
 				cb(result);
 			});
-		});
+		});*/
 
 		socket.on('/youtube/getVideos/:query', (query, cb) => {
 			core['/youtube/getVideos/:query'](query, result => {

+ 22 - 37
backend/logic/stations.js

@@ -9,7 +9,7 @@ module.exports = {
 		constructor(id, data) {
 			this.nsp = io.of(id);
 			let local = this;
-			this.nsp.on('connection', socket => {
+			this.nsp.on('connection', (socket, cb) => {
 				console.log('someone connected');
 				socket.on("pause", function() {
 					local.pause();
@@ -20,6 +20,15 @@ module.exports = {
 				socket.on("skipSong", function() {
 					local.skipSong();
 				});
+
+				socket.emit("connected", {
+					displayName: this.getDisplayName(),
+					users: this.getUsers().length,
+					currentSong: this.getCurrentSong(),
+					timePaused: this.getTimePaused(),
+					paused: this.isPaused(),
+					currentTime: Date.now()
+				});
 			});
 			this.id = id;
 
@@ -29,7 +38,7 @@ module.exports = {
 			this.paused = data.paused;
 			this.locked = data.locked;
 			this.skipVotes = 0;
-			this.sockets = [];
+			this.users = [];
 			this.displayName = data.displayName;
 			this.description = data.description;
 			this.timePaused = 0;
@@ -138,47 +147,23 @@ module.exports = {
 			return this.description;
 		}
 
-		getSockets() {
-			return this.sockets;
+		addUser(user) {
+			this.users.add(user);
+			this.nsp.emit("updateUsers", this.users);
 		}
 
-		getTimePaused() {
-			console.log(this.timePaused, "        ", this.timer.getTimePaused());
-			return this.timePaused + this.timer.getTimePaused();
+		removeUser(user) {
+			this.users.splice(this.users.indexOf(user), 1);
+			this.nsp.emit("updateUsers", this.users);
 		}
 
-		emitToStation(...data) {
-			this.sockets.forEach(function(socketId) {
-				let socket = global.getSocketFromId(socketId);
-				if (socket !== undefined) {
-					socket.emit.apply(data);
-				} else {
-					// Remove socket and emit it
-				}
-			});
+		getUsers() {
+			return this.users;
 		}
 
-		handleUserJoin(socketId) {
-			const socket = global.getSocketFromId(socketId);
-			if (socket !== undefined) {
-				//TODO Check if banned from room & check if allowed to join room
-				if (this.sockets.indexOf(socketId) === -1) {
-					this.emitToStation("userJoin", "Person");
-					this.sockets.push(socketId);
-					return {
-						displayName: this.getDisplayName(),
-						users: this.getSockets().length,
-						currentSong: this.getCurrentSong(),
-						timePaused: this.getTimePaused(),
-						paused: this.isPaused(),
-						currentTime: Date.now()
-					};
-				} else {
-					return {err: "Already in that station."};
-				}
-			} else {
-				return {err: "Invalid socket id."};
-			}
+		getTimePaused() {
+			console.log(this.timePaused, "        ", this.timer.getTimePaused());
+			return this.timePaused + this.timer.getTimePaused();
 		}
 	},
 	addStation: station => {

+ 1 - 1
backend/package.json

@@ -21,6 +21,6 @@
     "passport-local": "^1.0.0",
     "passport.socketio": "^3.6.2",
     "request": "^2.74.0",
-    "socket.io": "^1.4.8"
+    "socket.io": "^1.5.0"
   }
 }

+ 18 - 2
frontend/components/pages/Station.vue

@@ -104,6 +104,7 @@
 		methods: {
 			youtubeReady: function() {
 				let local = this;
+				console.log("YT Ready!!!");
 				local.player = new YT.Player("player", {
 					height: 270,
 					width: 480,
@@ -111,6 +112,7 @@
 					playerVars: {controls: 1, iv_load_policy: 3, rel: 0, showinfo: 0},
 					events: {
 						'onReady': function (event) {
+							console.log("Ready!!!");
 							local.playerReady = true;
 							let volume = parseInt(localStorage.getItem("volume"));
 							volume = (typeof volume === "number") ? volume : 20;
@@ -322,11 +324,24 @@
 		ready: function() {
 			let local = this;
 			window.onYouTubeIframeAPIReady = function() {
+				console.log("API READY?");
 				local.youtubeReady();
 			};
 
 			local.socket = this.$parent.socket;
 			local.stationSocket = io.connect('http://dev.musare.com/edm');
+			local.stationSocket.on("connected", function(data) {
+				console.log("JOINED!?");
+				local.currentSong = data.currentSong;
+				local.paused = data.paused;
+				local.timePaused = data.timePaused;
+				local.timePausedCurrentTime  = data.currentTime;
+				let tag = document.createElement('script');
+
+				tag.src = "https://www.youtube.com/iframe_api";
+				let firstScriptTag = document.getElementsByTagName('script')[0];
+				firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
+			});
 			local.stationSocket.on("skippedSong", function(currentSong) {
 				console.log("SKIPPED SONG");
 				local.currentSong = currentSong;
@@ -349,7 +364,8 @@
 			$("#volumeSlider").val(volume);
 
 			// TODO: Remove this
-			local.socket.emit("/stations/join/:id", "edm", function(data) {
+			/*local.socket.emit("/station/:id/join", "edm", function(data) {
+				console.log("JOINED!?");
 				local.currentSong = data.data.currentSong;
 				local.paused = data.data.paused;
 				local.timePaused = data.data.timePaused;
@@ -359,7 +375,7 @@
 				tag.src = "https://www.youtube.com/iframe_api";
 				let firstScriptTag = document.getElementsByTagName('script')[0];
 				firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
-			});
+			});*/
 		},
 		components: { StationHeader, MainFooter }
 	}