Sfoglia il codice sorgente

Added io to global and added some emit's for stations.

KrisVos130 7 anni fa
parent
commit
e5b0c181da
3 ha cambiato i file con 17 aggiunte e 6 eliminazioni
  1. 1 0
      src/app.js
  2. 1 0
      src/logic/global.js
  3. 15 6
      src/logic/stations.js

+ 1 - 0
src/app.js

@@ -32,6 +32,7 @@ r.connect( { host: 'localhost', port: 28015, db: 'musare' }, (err, rc) => {
 	else {
 
 		global.rc = rc;
+		global.io = io;
 
 		app.use(express.static(__dirname + '/public'));
 

+ 1 - 0
src/logic/global.js

@@ -31,6 +31,7 @@ function Timer(callback, delay, paused) {
 
 module.exports = {
 	rc: null, // RethinkDB Connection, this gets set in app.js
+	io: null, // Socket.io
 	htmlEntities: function(str) {
 		return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
 	},

+ 15 - 6
src/logic/stations.js

@@ -1,6 +1,7 @@
 
 // custom modules
 const global = require('./global');
+var io = global.io;
 
 function Station (id, data) {
 
@@ -17,6 +18,10 @@ function Station (id, data) {
 	var displayName = data.displayName;
 	var description = data.description;
 	var timer;
+	var nsp = io.of('/' + id);
+	nsp.on('connection', function(socket){
+		console.log('someone connected');
+	});
 
 	this.skipSong = function() {
 		if (playlist.length > 0) {
@@ -35,7 +40,7 @@ function Station (id, data) {
 				self.skipSong();
 			}, currentSong.duration, paused);
 
-			//io.emit("skipSong " + id, currentSong);
+			nsp.emit("skippedSong", currentSong);
 		}
 	};
 	this.toggleVoteSkip = function(userId) {
@@ -45,7 +50,7 @@ function Station (id, data) {
 			skipVotes = skipVotes.splice(skipVotes.indexOf(userId), 1);
 		}
 		//TODO Calculate if enough people voted to skip
-		//TODO Emit
+		nsp.emit("voteSkip", skipVotes);
 	};
 	this.retrievePlaylist = function() {
 		//TODO Use Rethink to get the Playlist for this station
@@ -54,15 +59,15 @@ function Station (id, data) {
 		if (!paused) {
 			paused = true;
 			timer.pause();
+			nsp.emit("pause");
 		}
-		//TODO Emit
 	};
 	this.unpause = function() {
 		if (paused) {
 			paused = false;
 			timer.resume();
+			nsp.emit("unpause");
 		}
-		//TODO Emit
 	};
 	this.isPaused = function() {
 		return paused;
@@ -73,14 +78,14 @@ function Station (id, data) {
 	this.lock = function() {
 		if (!locked) {
 			locked = true;
+			nsp.emit("lock");
 		}
-		//TODO Emit
 	};
 	this.unlock = function() {
 		if (locked) {
 			locked = false;
+			nsp.emit("unlocked");
 		}
-		//TODO Emit
 	};
 	this.isLocked = function() {
 		return locked;
@@ -88,10 +93,12 @@ function Station (id, data) {
 	this.updateDisplayName = function(newDisplayName) {
 		//TODO Update RethinkDB
 		displayName = newDisplayName;
+		nsp.emit("updateDisplayName", newDisplayName);
 	};
 	this.updateDescription = function(newDescription) {
 		//TODO Update RethinkDB
 		description = newDescription;
+		nsp.emit("updateDescription", newDescription);
 	};
 	this.getId = function() {
 		return id;
@@ -104,9 +111,11 @@ function Station (id, data) {
 	};
 	this.addUser = function(user) {
 		users.add(user);
+		nsp.emit("updateUsers", users);
 	};
 	this.removeUser = function(user) {
 		users.splice(users.indexOf(user), 1);
+		nsp.emit("updateUsers", users);
 	};
 	this.getUsers = function() {
 		return users;