diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2014-10-23 20:11:57 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2014-10-23 20:11:57 +0800 |
commit | 66e439b6f48c1b030fd7a2f12a9d977d82f4fe35 (patch) | |
tree | e463aaf3bf6281bf3843348cc91a9ceaabb0887a /http.js | |
parent | f9ca054314351b7aba61c2acb70c1b3159536c8c (diff) | |
download | dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.tar dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.tar.gz dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.tar.bz2 dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.tar.lz dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.tar.xz dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.tar.zst dexon-66e439b6f48c1b030fd7a2f12a9d977d82f4fe35.zip |
http polling
Diffstat (limited to 'http.js')
-rw-r--r-- | http.js | 31 |
1 files changed, 25 insertions, 6 deletions
@@ -22,22 +22,41 @@ }; }; - HttpProvider.prototype.send = function (payload) { + HttpProvider.prototype.sendRequest = function (payload, cb) { var data = formatJsonRpcObject(payload); var request = new XMLHttpRequest(); request.open("POST", this.host, true); request.send(JSON.stringify(data)); - var self = this; request.onreadystatechange = function () { - if (request.readyState === 4) { - self.handlers.forEach(function (handler) { - handler.call(self, formatJsonRpcMessage(request.responseText)); - }); + if (request.readyState === 4 && cb) { + cb(request); } } }; + HttpProvider.prototype.send = function (payload) { + var self = this; + this.sendRequest(payload, function (request) { + self.handlers.forEach(function (handler) { + handler.call(self, formatJsonRpcMessage(request.responseText)); + }); + }); + }; + + HttpProvider.prototype.poll = function (payload, id) { + var self = this; + this.sendRequest(payload, function (request) { + var parsed = JSON.parse(request.responseText); + if (!parsed.result) { + return; + } + self.handlers.forEach(function (handler) { + handler.call(self, {_event: "messages", data: id}); + }); + }); + }; + Object.defineProperty(HttpProvider.prototype, "onmessage", { set: function (handler) { this.handlers.push(handler); |