Procházet zdrojové kódy

Worked on stations. (WIP)

Kris před 8 roky
rodič
revize
3d57634de1

+ 15 - 11
backend/logic/actions/stations.js

@@ -46,18 +46,14 @@ function initializeAndReturnStation (stationId, cb) {
 			cache.hget('stations', station.id, (err, station) => {
 				if (station) {
 					// notify all the sockets on this station to go to the next song
-					async.waterfall(io.sockets.clients().map((socket) => (next) => {
-						// fetch the sockets session
-						cache.hget('sessions', socket.sessionId, (err, session) => {
-							if (session.stationId == station.id) {
-								socket.emit('notification:stations.nextSong');
-							}
-							next();
-						});
-					}), (err) => {
-						// schedule a notification to be dispatched when the next song ends
-						notifications.schedule(`stations.nextSong?id=${station.id}`, 5000);
+					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 {
@@ -124,6 +120,8 @@ 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) {
@@ -134,8 +132,12 @@ 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
+
 				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);
+					//TODO Emit to cache, listen on cache
 					cb({ status: 'success', userCount });
 				});
 			}
@@ -200,6 +202,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);
 					cb({ status: 'success', userCount });
 				});
 			} else {
@@ -241,6 +244,7 @@ module.exports = {
 					console.error(err);
 					return cb({ status: 'error', message: 'Failed to save song from youtube to the database' });
 				}
+				//TODO Emit to cache, and listen on cache
 
 				// stations.getStation(station).playlist.push(newSong);
 

+ 2 - 0
backend/logic/io.js

@@ -13,6 +13,7 @@ module.exports = {
 
 	init: (cb) => {
 
+		//TODO Check every 30s/60s, for all sockets, if they are still allowed to be in the rooms they are in, and on socket at all (permission changing/banning)
 		this.io = require('socket.io')(app.server);
 
 		this.io.use((socket, next) => {
@@ -24,6 +25,7 @@ module.exports = {
 
 		this.io.on('connection', socket => {
 
+			socket.join("SomeRoom");
 			console.log("io: User has connected");
 
 			// catch when the socket has been disconnected

+ 26 - 1
backend/logic/utils.js

@@ -1,6 +1,7 @@
 'use strict';
 
-const moment = require('moment');
+const 	moment = require('moment'),
+		io = require('./io');
 
 class Timer {
 	constructor(callback, delay, paused) {
@@ -126,5 +127,29 @@ module.exports = {
 			delete cookies[cookieName];
 			return this.toString(cookies);
 		}
+	},
+	socketFromSession: sessionId => {
+		let sockets = io.io.sockets;
+		for (let i = 0; i < sockets.length; i++) {
+			let socket = sockets[i];
+			if (socket.sessionId === sessionId) {
+				return socket;
+			}
+		}
+	},
+	socketLeaveRooms: (sessionId, room) => {
+		let socket = this.socketFromSession(sessionId);
+		let rooms = io.sockets.manager.roomClients[socket.id];
+		for (let j = 0; j < rooms.length; j++) {
+			socket.leave(rooms[j]);
+		}
+	},
+	socketJoinRoom: (sessionId, room) => {
+		let socket = this.socketFromSession(sessionId);
+		let rooms = io.sockets.manager.roomClients[socket.id];
+		for (let j = 0; j < rooms.length; j++) {
+			socket.leave(rooms[j]);
+		}
+		socket.join(room);
 	}
 };

+ 6 - 15
frontend/App.vue

@@ -21,13 +21,7 @@
 					email: "",
 					password: ""
 				},
-				likes: [],
-				dislikes: [],
-				loggedIn: false,
-				stations: {
-					official: [],
-					community: []
-				}
+				loggedIn: false
 			}
 		},
 		methods: {
@@ -42,11 +36,8 @@
 			lofig.get('socket.url', function(res) {
 				let socket = _this.socket = io(window.location.protocol + '//' + res);
 				socket.on("ready", status => _this.loggedIn = status);
-				socket.emit("stations.index", data => {
-					if (data.status === "success")  data.stations.forEach(station => {
-						if (station.type == 'official') _this.stations.official.push(station);
-						else _this.stations.community.push(station);
-					});
+				socket.on("SomeRoomMessage", function() {
+				    console.log("SOME ROOM MESSAGE!");
 				});
 			});
 		},
@@ -73,8 +64,8 @@
 						//TODO Error toast
 					}
 				});
-			},
-			'joinStation': function (id) {
+			}
+			/*'joinStation': function (id) {
 				let mergedStations = this.stations.community.concat(this.stations.official);
 				this.socket.emit('stations.join', id, result => {
 					mergedStations.find(station => station.id === id).users = result.userCount;
@@ -84,7 +75,7 @@
 				this.socket.emit('stations.leave', result => {
 					//this.stations.find(station => station.id === id).users = result.userCount;
 				});
-			}
+			}*/
 		},
 		components: { WhatIsNew }
 	}

+ 16 - 30
frontend/components/Station/Station.vue

@@ -235,41 +235,27 @@
 		},
 		ready: function() {
 			let _this = this;
+			_this.stationId = _this.$route.params.id;
 
 			_this.interval = 0;
 
 			_this.socket = _this.$parent.socket;
 
-			/*_this.socket.on('event:songs.next', data => {
-				let { currentSong, startedAt } = data;
-				this.currentSong = currentSong;
-				this.startedAt = startedAt;
-				this.timePaused = 0;
-				this.playVideo();
-			});*/
-
-			lofig.get('socket.url', res => {
-				_this.stationSocket = io(window.location.protocol + '//' + res + '/' + _this.$route.params.id);
-
-				_this.stationSocket.on("connected", (data) => {
-					console.log(data);
-					_this.currentSong = data.currentSong;
-					_this.startedAt = data.startedAt;
-					_this.paused = data.paused;
-					_this.timePaused = data.timePaused;
-					_this.currentTime  = data.currentTime;
-					_this.youtubeReady();
-				});
-				
-				this.youtubeReady();
-
-				_this.stationSocket.on("nextSong", (currentSong, startedAt) => {
-					console.log(currentSong, startedAt);
-					_this.currentSong = currentSong;
-					_this.startedAt = startedAt;
-					_this.timePaused = 0;
-					_this.playVideo();
-				});
+			_this.socket.emit('stations.join', _this.stationId, data => {
+				_this.currentSong = data.currentSong;
+				_this.startedAt = data.startedAt;
+				_this.paused = data.paused;
+				_this.timePaused = data.timePaused;
+				_this.youtubeReady();
+				_this.playVideo();
+			});
+
+			_this.socket.on('event:songs.next', data => {
+				_this.currentSong = data.currentSong;
+				_this.startedAt = data.startedAt;
+				_this.paused = data.paused;
+				_this.timePaused = data.timePaused;
+				_this.playVideo();
 			});
 
 			let volume = parseInt(localStorage.getItem("volume"));

+ 12 - 0
frontend/components/pages/Home.vue

@@ -101,6 +101,10 @@
 				isLoginActive: false,
 				recaptcha: {
 					key: ''
+				},
+				stations: {
+					official: [],
+					community: []
 				}
 			}
 		},
@@ -109,6 +113,14 @@
 			lofig.get('recaptcha.key', function(key) {
 				_this.recaptcha.key = key;
 			});
+			socket.emit("stations.index", data => {
+				if (data.status === "success")  data.stations.forEach(station => {
+					if (station.type == 'official') _this.stations.official.push(station);
+					else _this.stations.community.push(station);
+				});
+			});
+			socket.emit("");
+			socket.on("");
 		},
 		methods: {
 			toggleModal: function(type) {