Skip to content

Commit

Permalink
Add the http-poll service
Browse files Browse the repository at this point in the history
  • Loading branch information
enricostara committed Aug 22, 2015
1 parent 4cd0bbe commit f16c420
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/telegram.link.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,64 @@ Client.prototype.getDataCenters = function (callback) {


// ***
// client.**httpPoll(callback, [maxWait], [waitAfter], [maxDelay])**

// HTTP long poll service.

// The code:
Client.prototype.httpPoll = function (callback, maxWait, waitAfter, maxDelay) {
if (callback) {
this.once('httpPoll', callback);
}
if (this.isReady(true) && this._connection instanceof mt.net.HttpConnection) {
var self = this;
maxWait = maxWait || 30000;
console.log('call http long poll each %sms', maxWait);
try {
mt.service.http_wait({
props: {
max_delay: maxDelay || 0,
wait_after: waitAfter || 0,
max_wait: maxWait
},
channel: self._channel,
callback: function (ex, result) {
if (ex) {
self.emit('error', ex);
} else {
self.emit('httpPoll', result);
if(self._httpPollLoop) {
self.httpPoll(callback, maxWait, waitAfter, maxDelay);
}
}
}
});
} catch (err) {
this.emit('error', err);
}
}
};

// ***
// client.**startHttpPollLoop(callback, [maxWait], [waitAfter], [maxDelay])**

// Start the HTTP long poll service loop.

// The code:
Client.prototype.startHttpPollLoop = function (callback, maxWait, waitAfter, maxDelay) {
this._httpPollLoop = true;
this.httpPoll(callback, maxWait, waitAfter, maxDelay);
};

// ***
// client.**stopHttpPollLoop()**

// Stop the HTTP long poll service loop.

// The code:
Client.prototype.stopHttpPollLoop = function () {
this._httpPollLoop = false;
};
// client.**isReady()**

// Checks if the client is ready to communicate with Telegram and immediately emits the error-event if required.
Expand Down
45 changes: 45 additions & 0 deletions test/telegram.link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('requirish')._(module);
var telegramLink = require('lib/telegram.link')();
var net = require('telegram-mt-node').net;
var api = require('lib/api');
var mt = require('telegram-mt-node');


describe('TelegramLink', function () {
Expand Down Expand Up @@ -140,4 +141,48 @@ describe('TelegramLink', function () {
});
});
});

describe('#start/stop httpPoll()', function () {
mt.service.http_wait = function (input) {
input.callback(null, {});
};

it('should returns ok', function (done) {
var client = telegramLink.createClient({authKey: {}}, primaryDC, function () {
client.httpPoll(function (result) {
result.should.be.ok;
done();
})
});
});

it('should start', function (done) {
var client = telegramLink.createClient({authKey: {}}, primaryDC, function () {
mt.service.http_wait = function (input) {
client._httpPollLoop = false;
input.callback(null, {});
};
client.startHttpPollLoop(function (result) {
result.should.be.ok;
client._httpPollLoop.should.be.false();
done();
});
client._httpPollLoop.should.be.true();
});
});

it('should start', function (done) {
var client = telegramLink.createClient({authKey: {}}, primaryDC, function () {
mt.service.http_wait = function (input) {
client._httpPollLoop = false;
input.callback(null, {});
};
client.startHttpPollLoop();
client.stopHttpPollLoop();
client._httpPollLoop.should.be.false();
done();
});
});
});

});

0 comments on commit f16c420

Please sign in to comment.