Use socket.io rooms instead of manually implementing sendAll

Should fix Issue#9
This commit is contained in:
calzoneman 2013-03-20 13:03:32 -05:00
parent 1bc20fe947
commit e76f149916
2 changed files with 15 additions and 8 deletions

View File

@ -13,6 +13,7 @@ var Rank = require('./rank.js');
var InfoGetter = require('./get-info.js'); var InfoGetter = require('./get-info.js');
var Media = require('./media.js').Media; var Media = require('./media.js').Media;
var ChatCommand = require('./chatcommand.js'); var ChatCommand = require('./chatcommand.js');
var io = require('./server.js').io;
var Channel = function(name) { var Channel = function(name) {
console.log("Opening channel " + name); console.log("Opening channel " + name);
@ -253,6 +254,7 @@ Channel.prototype.searchLibrary = function(query) {
// Called when a new user enters the channel // Called when a new user enters the channel
Channel.prototype.userJoin = function(user) { Channel.prototype.userJoin = function(user) {
user.socket.join(this.name);
// Prevent duplicate login // Prevent duplicate login
if(user.name != "") { if(user.name != "") {
for(var i = 0; i < this.users.length; i++) { for(var i = 0; i < this.users.length; i++) {
@ -287,11 +289,15 @@ Channel.prototype.userJoin = function(user) {
} }
if(user.playerReady) if(user.playerReady)
this.sendMediaUpdate(user); 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 // Called when a user leaves the channel
Channel.prototype.userLeave = function(user) { Channel.prototype.userLeave = function(user) {
try {
user.socket.leave(this.name);
}
catch(e) {}
if(this.poll) { if(this.poll) {
this.poll.unvote(user.ip); this.poll.unvote(user.ip);
this.broadcastPollUpdate(); this.broadcastPollUpdate();
@ -573,8 +579,11 @@ Channel.prototype.changeLeader = function(name) {
this.broadcastRankUpdate(old); this.broadcastRankUpdate(old);
} }
if(name == "") { if(name == "") {
if(this.currentMedia != null) if(this.currentMedia != null) {
this.time = new Date().getTime();
this.i = 0;
channelVideoUpdate(this, this.currentMedia.id); channelVideoUpdate(this, this.currentMedia.id);
}
return; return;
} }
for(var i = 0; i < this.users.length; i++) { for(var i = 0; i < this.users.length; i++) {
@ -667,9 +676,7 @@ Channel.prototype.broadcastPollClose = function() {
// Send to ALL the clients! // Send to ALL the clients!
Channel.prototype.sendAll = function(message, data) { Channel.prototype.sendAll = function(message, data) {
for(var i = 0; i < this.users.length; i++) { io.sockets.in(this.name).emit(message, data);
this.users[i].socket.emit(message, data);
}
} }
// Autolead yay // Autolead yay

View File

@ -6,17 +6,17 @@
* *
*/ */
var User = require('./user.js').User;
var Config = require('./config.js'); var Config = require('./config.js');
var connect = require('connect'); var connect = require('connect');
var app = connect.createServer(connect.static(__dirname+'/www')).listen(Config.IO_PORT); 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'); var Database = require('./database.js');
Database.init(); Database.init();
exports.channels = {}; exports.channels = {};
io.sockets.on('connection', function(socket) { exports.io.sockets.on('connection', function(socket) {
var user = new User(socket, socket.handshake.address.address); var user = new User(socket, socket.handshake.address.address);
console.log('New connection from /' + user.ip); console.log('New connection from /' + user.ip);
}); });