aboutsummaryrefslogtreecommitdiffstats
path: root/http.js
diff options
context:
space:
mode:
Diffstat (limited to 'http.js')
-rw-r--r--http.js31
1 files changed, 25 insertions, 6 deletions
diff --git a/http.js b/http.js
index ff8335657..768d7c1ff 100644
--- a/http.js
+++ b/http.js
@@ -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);