From f16c420211258a851434b1092a267fc16e64d6ed Mon Sep 17 00:00:00 2001 From: Enrico Stara Date: Sat, 22 Aug 2015 18:45:05 +0200 Subject: [PATCH] Add the http-poll service --- lib/telegram.link.js | 58 ++++++++++++++++++++++++++++++++++++++ test/telegram.link.test.js | 45 +++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/lib/telegram.link.js b/lib/telegram.link.js index 264c4a6..a9e3b38 100644 --- a/lib/telegram.link.js +++ b/lib/telegram.link.js @@ -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. diff --git a/test/telegram.link.test.js b/test/telegram.link.test.js index 617020a..65757f3 100644 --- a/test/telegram.link.test.js +++ b/test/telegram.link.test.js @@ -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 () { @@ -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(); + }); + }); + }); + });