diff --git a/channel.js b/channel.js index fff37c51..3d85a15a 100644 --- a/channel.js +++ b/channel.js @@ -13,6 +13,7 @@ var Rank = require('./rank.js'); var InfoGetter = require('./get-info.js'); var Media = require('./media.js').Media; var ChatCommand = require('./chatcommand.js'); +var io = require('./server.js').io; var Channel = function(name) { console.log("Opening channel " + name); @@ -253,6 +254,7 @@ Channel.prototype.searchLibrary = function(query) { // Called when a new user enters the channel Channel.prototype.userJoin = function(user) { + user.socket.join(this.name); // Prevent duplicate login if(user.name != "") { for(var i = 0; i < this.users.length; i++) { @@ -287,11 +289,15 @@ Channel.prototype.userJoin = function(user) { } if(user.playerReady) this.sendMediaUpdate(user); - console.log(user.ip + " joined channel " + this.name); + console.log("/" + user.ip + " joined channel " + this.name); } // Called when a user leaves the channel Channel.prototype.userLeave = function(user) { + try { + user.socket.leave(this.name); + } + catch(e) {} if(this.poll) { this.poll.unvote(user.ip); this.broadcastPollUpdate(); @@ -573,8 +579,11 @@ Channel.prototype.changeLeader = function(name) { this.broadcastRankUpdate(old); } if(name == "") { - if(this.currentMedia != null) + if(this.currentMedia != null) { + this.time = new Date().getTime(); + this.i = 0; channelVideoUpdate(this, this.currentMedia.id); + } return; } for(var i = 0; i < this.users.length; i++) { @@ -667,9 +676,7 @@ Channel.prototype.broadcastPollClose = function() { // Send to ALL the clients! Channel.prototype.sendAll = function(message, data) { - for(var i = 0; i < this.users.length; i++) { - this.users[i].socket.emit(message, data); - } + io.sockets.in(this.name).emit(message, data); } // Autolead yay diff --git a/server.js b/server.js index d721f4b0..3b0ba6f2 100644 --- a/server.js +++ b/server.js @@ -6,17 +6,17 @@ * */ -var User = require('./user.js').User; var Config = require('./config.js'); var connect = require('connect'); var app = connect.createServer(connect.static(__dirname+'/www')).listen(Config.IO_PORT); -var io = require('socket.io').listen(app); +exports.io = require('socket.io').listen(app); +var User = require('./user.js').User; var Database = require('./database.js'); Database.init(); exports.channels = {}; -io.sockets.on('connection', function(socket) { +exports.io.sockets.on('connection', function(socket) { var user = new User(socket, socket.handshake.address.address); console.log('New connection from /' + user.ip); });