Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions src/bbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ BBT = function(key_id, options) {
BBT.instances.push(this);

this.connection = new BBT.Connection(this);
this.connect();

if (options.auto_connect === true) {
this.connect();
}

}

/*** Constant Values ***/
Expand Down Expand Up @@ -181,6 +184,8 @@ BBT.prototype.updateParams = function(params) {
if(params.port) this.port = params.port;
if(params.sport) this.sport = params.sport;
if(params.ssl === false) this.ssl = params.ssl; //set to false
if(params.on_connect) this.on_connect = params.on_connect;
if(params.on_disconnect) this.on_disconnect = params.on_disconnect;

if(params.cipher) this.cipher = params.cipher;
}
Expand All @@ -204,21 +209,35 @@ BBT.Connection = function(bbt) {
}

BBT.Connection.prototype.onConnection = function() {
if (this.bbt.on_connect) {
this.bbt.on_connect();
return
}
for(c in this.channels.channels) {
this.channels.channels[c].do_subscribe();
}
}

BBT.Connection.prototype.onDisonnection = function() {
BBT.warn("Connetion has been Dropped");

if (this.bbt.on_disconnect) {
this.bbt.on_disconnect();
return
}
}

BBT.Connection.prototype.connect = function () {
if( this.connection && this.connection.connected ) return this;
var self = this;
var query = 'key=' + this.bbt.key + '&username=' + (self.bbt.userinfo.username || '');
this.connection = io(self.bbt.getWsUrl(), {query: query });
this.connection = io(self.bbt.getWsUrl(), {query: query});
this.connection.on('connect', function () {
self.onConnection();
});

this.connection.on('disconnect', function () {
self.onDisonnection();
});

this.connection.on('message', function (msg) {
Expand Down Expand Up @@ -270,7 +289,6 @@ BBT.Connection.prototype.subscribe = function(args, callback) {
}
}


BBT.Connection.prototype.unsubscribe = function(args) {
var Channel = this.channels.get(args.channel, args.resource);
if(Channel) {
Expand All @@ -285,8 +303,8 @@ BBT.Connection.prototype.publish = function(args) {

if(Channel && Channel.hasWritePermission()) {
if(this.send('stream', 'emit', {channel: args.channel, resource: args.resource, data: args.data})) {
return args.callback(null, {code: 0});
}else {
return args.callback({code: 0});
} else {
return args.callback({code: 11, message: 'Error while publishing message!'});
}
}
Expand All @@ -298,8 +316,8 @@ BBT.Connection.prototype.write = function(args) {

if(Channel && Channel.hasWritePermission()) {
if(this.send('stream', 'write', {channel: args.channel, resource: args.resource, data: args.data})) {
return args.callback(null, {code: 0});
}else {
return args.callback({code: 0});
} else {
return args.callback({code: 11, message: 'Error while writing message!'});
}
}
Expand Down Expand Up @@ -353,7 +371,7 @@ BBT.Channels.prototype.getChannelWithPermission = function(channel, resource, re
if(read) match = Channel.hasReadPermission();
if(write) match = Channel.hasWritePermission();
if(match) return Channel;
}else if(Channel = this.channels[channel + '.*']) {
} else if (Channel = this.channels[channel + '.*']) {
match = true;
if(read) match = Channel.hasReadPermission();
if(write) match = Channel.hasWritePermission();
Expand All @@ -372,6 +390,7 @@ BBT.Channel = function(args, fct, bbt) {
this.subscribed = false;
this.write = args.write || false;
this.read = args.read || false;
this.is_public = args.is_public || false;
this.writePermission = false;
this.readPermission = false;
this.onError = args.onError;
Expand Down Expand Up @@ -403,15 +422,22 @@ BBT.Channel.prototype.update = function(args) {

//Authentication required for write access and for read access to private or presence resources
BBT.Channel.prototype.authNeeded = function() {
if(this.write === true) return true;
if(this.channel.indexOf('private-') === 0) return true;
if(this.channel.indexOf('presence-') === 0) return true;
if(!this.is_public) {
if(this.write === true) return true;
if(this.channel.indexOf('private-') === 0) return true;
if(this.channel.indexOf('presence-') === 0) return true;
}

return false;
}

BBT.Channel.prototype.do_subscribe = function() {
var self = this;
if(!self.bbt.connection.connection.connected) return;
if(!self.bbt.connection.connection.connected) {
BBT.warn("Cannot subscribe as not connected");
return;
}

var connection = this.bbt.connection;

var args = {};
Expand All @@ -429,8 +455,8 @@ BBT.Channel.prototype.do_subscribe = function() {
if(connection.connection && connection.connection.connected && connection.connection.io.engine.id) {
args.sid = connection.connection.io.engine.id;
if(connection.bbt.auth_method === 'get') {
$.get( connection.bbt.auth_endpoint, args )
.success(function( data ) {
$.getJSON( connection.bbt.auth_endpoint, args)
.done(function(data, status) {
if(!data.auth) {
return self.onError('Bad authentication reply');
}
Expand All @@ -444,7 +470,7 @@ BBT.Channel.prototype.do_subscribe = function() {
return false;
}
})
.error(function(XMLHttpRequest, textStatus, errorThrown) {
.fail(function(XMLHttpRequest, textStatus, errorThrown) {
return self.onError('Unable to authenticate client');
});
}else if (connection.bbt.auth_method === 'post') {
Expand Down Expand Up @@ -482,7 +508,7 @@ BBT.Channel.prototype.do_subscribe = function() {
} else {
return self.onError('Connection error encountered');
}
}else {
} else {
if(connection.send('control', 'subscribe', args)) {
self.subscribe();
self.onSuccess('Successfully subscribed to ' + self.channel + '.' + self.resource);
Expand Down Expand Up @@ -540,7 +566,7 @@ BBT.Channel.prototype.hasReadPermission = function() {
}

function checkAppKey(key) {
if (key === null || key === undefined) {
if (key === null || key === undefined || key === "") {
BBT.warn(
'Warning: You must pass your key id when you instantiate BBT.'
);
Expand Down Expand Up @@ -665,7 +691,8 @@ BBT.prototype.subscribe = function(args, callback) {
vargs.read = (typeof args.read === 'undefined' ) ? true : args.read === true; //default true
vargs.write = args.write === true; // default false
vargs.onError = args.onError || BBT.warn;
vargs.onSuccess = args.onSuccess || function() {};
vargs.onSuccess = args.onSuccess || function(msg) {console.log(msg);};
vargs.is_public = args.is_public || false;
var onError = vargs.onError;

if(!vargs.channel) return onError('channel not specified');
Expand Down Expand Up @@ -693,7 +720,7 @@ BBT.prototype.unsubscribe = function(args) {
vargs.channel = args.channel;
vargs.resource = args.resource || '*';
vargs.onError = args.onError || BBT.warn;
vargs.onSuccess = args.onSuccess || function() {};
vargs.onSuccess = args.onSuccess || function(msg) {console.log(msg);};

if(!vargs.channel) return vargs.onError('channel not specified');
if(!(typeof vargs.channel === 'string')) return vargs.onError('Invalid format: channel must be a string');
Expand Down