Skip to content

Commit 78423d9

Browse files
committed
New tests, fix 7549
1 parent e1c1ff7 commit 78423d9

2 files changed

Lines changed: 48 additions & 22 deletions

File tree

ClearBlade.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ if (!window.console) {
8686
* @property URI
8787
* @type String
8888
*/
89-
ClearBlade.URI = options.URI || "https://platform.clearblade.com";
89+
ClearBlade.URI = options.URI || "http://162.209.79.118";
9090
/**
9191
* This is the property that tells the API whether or not the API will log to the console
9292
* This should be left `false` in production
@@ -950,12 +950,13 @@ if (!window.console) {
950950
* <p>{boolean} [cleanSession] The server will persist state of the session if true. Not avaliable in beta.</p>
951951
* <p>{boolean} [useSSL] The option to use SSL websockets. Default is false for now.</p>
952952
* <p>{object} [invocationContext] An object to wrap all the important variables needed for the onFalure and onSuccess functions. The default is empty.</p>
953-
* <p>{function} [onSuccess] A callback to operate on the result of a sucessful connect. In beta the default is empty.</p>
954-
* <p>{function} [onFailure] A callback to operate on the result of an unsuccessful connect. In beta the default is also empty.</p>
953+
* <p>{function} [onSuccess] A callback to operate on the result of a sucessful connect. In beta the default is just the invoking of the `callback` parameter with the data from the connection.</p>
954+
* <p>{function} [onFailure] A callback to operate on the result of an unsuccessful connect. In beta the default is just the invoking of the `callback` parameter with the data from the connection.</p>
955955
* <p>{Object} [hosts] An array of hosts to attempt to connect too. Sticks to the first one that works. The default is "platform.clearblade.com".</p>
956956
* <p>{Object} [ports] An array of ports to try, it also sticks to thef first one that works. The defaults are 80,8080,1337.</p>
957957
*</p>
958-
* @param {function} callback Callback to be run upon connection
958+
* @param {function} callback Callback to be run upon either succeessful or
959+
* failed connection
959960
* @example <caption> A standard connect</caption>
960961
* var callback = function (data) {
961962
* console.log(data);
@@ -971,35 +972,37 @@ if (!window.console) {
971972
conf.password = ClearBlade.appSecret;
972973
conf.cleanSession = options.cleanSession || true;
973974
conf.useSSL = options.useSSL || false; //up for debate. ole' perf vs sec argument
974-
conf.hosts = ["platform.clearblade.com"];
975+
conf.hosts = ["64.49.226.26"];
975976
conf.ports = [1337];
976977

977978
var onConnectionLost = function(){
978-
alert("connection lost- attempting to reestablish");
979+
console.log("ClearBlade Messaging connection lost- attempting to reestablish");
979980
that.client.connect(conf);
980981
};
981982

982983
var onMessageArrived = function(message){
983-
console.log("message arrived: "+message.payloadString);
984+
// messageCallback from Subscribe()
984985
that.messageCallback(message.payloadString);
985986
};
986987

987988
var clientID = Math.floor(Math.random() * 10e12).toString();
988989
this.client = new Messaging.Client(conf.hosts[0],conf.ports[0],clientID);
989990
this.client.onConnectionLost = onConnectionLost;
990991
this.client.onMessageArrived = onMessageArrived;
991-
var onConnect = function(data) {
992+
// the mqtt websocket library uses "onConnect," but our terminology uses
993+
// "onSuccess" and "onFailure"
994+
var onSuccess = function(data) {
992995
callback(data);
993996
};
994997

995-
this.client.onConnect = onConnect;
998+
this.client.onConnect = onSuccess;
996999
var onFailure = function(err) {
997-
alert("failed to connect");
1000+
console.log("ClearBlade Messaging failed to connect");
9981001
callback(err);
9991002
};
10001003

1001-
conf.onSuccess = onConnect;
1002-
conf.onFailure = onFailure;
1004+
conf.onSuccess = options.onSuccess || onSuccess;
1005+
conf.onFailure = options.onFailure || onFailure;
10031006

10041007
this.client.connect(conf);
10051008
};

test/ClearBladeSpec.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ describe("Query objects should", function () {
333333
describe("The ClearBlade Messaging module", function() {
334334
var flag, messaging, msgReceived;
335335

336+
var onMessageArrived = function(message) {
337+
flag = true;
338+
msgReceived = message;
339+
};
340+
var onConnect = function(data) {
341+
flag = true;
342+
// Once a connection has been made, make a subscription and send a message.
343+
messaging.Subscribe('/test', {}, onMessageArrived);
344+
};
345+
336346
beforeEach(function () {
337347
var initOptions = {
338348
appKey: 'c49ee8a80ae2e3d5b4edfaa7eb75',
@@ -342,16 +352,6 @@ describe("The ClearBlade Messaging module", function() {
342352
});
343353

344354
it("should be able to subscribe and send/receive a message", function () {
345-
var onMessageArrived = function(message) {
346-
flag = true;
347-
msgReceived = message;
348-
};
349-
var onConnect = function(data) {
350-
flag = true;
351-
// Once a connection has been made, make a subscription and send a message.
352-
messaging.Subscribe('/test', {}, onMessageArrived);
353-
};
354-
355355
runs(function() {
356356
flag = false;
357357
messaging = new ClearBlade.Messaging({}, onConnect);
@@ -374,4 +374,27 @@ describe("The ClearBlade Messaging module", function() {
374374
expect(msgReceived).toEqual('hello');
375375
});
376376
});
377+
378+
it("should use the callbacks I pass into Subscribe()", function () {
379+
var successMsg;
380+
381+
// Custom success callback to use in Subscribe options
382+
var onSuccess = function(data) {
383+
flag = true;
384+
successMsg = 'EXECUTED';
385+
};
386+
387+
runs(function() {
388+
flag = false;
389+
messaging = new ClearBlade.Messaging({onSuccess:onSuccess}, onConnect);
390+
});
391+
392+
waitsFor(function() {
393+
return flag;
394+
}, "Did not connect", 3000);
395+
396+
runs(function() {
397+
expect(successMsg).toEqual('EXECUTED');
398+
});
399+
});
377400
});

0 commit comments

Comments
 (0)