diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2014-10-22 17:38:33 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2014-10-22 17:41:48 +0800 |
commit | 5cd93a061889b0f2b9047ed6798f91aeb132dcea (patch) | |
tree | cac772593418fb941c34c55c834ed2f545f46289 /http.js | |
parent | eef4cd1b64c38e3327dbe7f1b70a60b01ce9cbed (diff) | |
download | dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.tar dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.tar.gz dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.tar.bz2 dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.tar.lz dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.tar.xz dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.tar.zst dexon-5cd93a061889b0f2b9047ed6798f91aeb132dcea.zip |
http provider
Diffstat (limited to 'http.js')
-rw-r--r-- | http.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/http.js b/http.js new file mode 100644 index 000000000..88869c689 --- /dev/null +++ b/http.js @@ -0,0 +1,52 @@ +(function () { + var HttpProvider = function (host) { + this.handlers = []; + this.host = host; + }; + + //TODO unify the format of object passed to 'send method' + function formatJsonRpcObject(object) { + return { + jsonrpc: '2.0', + method: object.call, + params: object.args, + id: object._id + } + }; + + //TODO unify the format of output messages, maybe there should be objects instead + function formatJsonRpcMessage(message) { + var object = JSON.parse(message); + + return JSON.stringify({ + _id: object.id, + data: object.result + }); + }; + + HttpProvider.prototype.send = function (payload) { + 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)); + }); + } + } + }; + + Object.defineProperty(HttpProvider.prototype, "onmessage", { + set: function (handler) { + this.handlers.push(handler); + } + }); + + if (typeof(web3) !== "undefined" && web3.providers !== undefined) { + web3.providers.HttpProvider = HttpProvider; + } +})(); |