diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-16 00:27:07 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-16 00:27:07 +0800 |
commit | 6d02c0d392762203dc150203ce0f315654aed5c2 (patch) | |
tree | e6c51635dae8245effc9b570de4f8cb00a117b1c /lib/websocket.js | |
parent | 508f116738517efc8c21233e9d50349ba30e223c (diff) | |
parent | ec74fc05d438806ece64fe34b0f28c8f45f5167e (diff) | |
download | go-tangerine-6d02c0d392762203dc150203ce0f315654aed5c2.tar.gz go-tangerine-6d02c0d392762203dc150203ce0f315654aed5c2.tar.zst go-tangerine-6d02c0d392762203dc150203ce0f315654aed5c2.zip |
Merge commit '1a6dbeff6e86d65cae6d7db366cbaa4182eaff7f' into ethereumjs
Conflicts:
libjsqrc/ethereumjs/dist/ethereum.js
libjsqrc/ethereumjs/dist/ethereum.js.map
libjsqrc/ethereumjs/dist/ethereum.min.js
libjsqrc/ethereumjs/lib/abi.js
Diffstat (limited to 'lib/websocket.js')
-rw-r--r-- | lib/websocket.js | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/websocket.js b/lib/websocket.js index 5b40075e4..e8663ecf5 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -27,9 +27,17 @@ if (process.env.NODE_ENV !== 'build') { var WebSocket = require('ws'); // jshint ignore:line } +/** + * WebSocketProvider object prototype is implementing 'provider protocol' + * Should be used when we want to connect to ethereum backend over websockets + * It's compatible with go client + * The constructor allows to specify host uri + */ var WebSocketProvider = function(host) { + // onmessage handlers this.handlers = []; + // queue will be filled with messages if send is invoked before the ws is ready this.queued = []; this.ready = false; @@ -46,15 +54,20 @@ var WebSocketProvider = function(host) { this.ws.onopen = function() { self.ready = true; - for(var i = 0; i < self.queued.length; i++) { + for (var i = 0; i < self.queued.length; i++) { // Resend self.send(self.queued[i]); } }; }; +/// Prototype object method +/// Should be called when we want to send single api request to server +/// Asynchronous, it's using websockets +/// Response for the call will be received by ws.onmessage +/// @param payload is inner message object WebSocketProvider.prototype.send = function(payload) { - if(this.ready) { + if (this.ready) { var data = JSON.stringify(payload); this.ws.send(data); @@ -63,13 +76,20 @@ WebSocketProvider.prototype.send = function(payload) { } }; +/// Prototype object method +/// Should be called to add handlers WebSocketProvider.prototype.onMessage = function(handler) { this.handlers.push(handler); }; +/// Prototype object method +/// Should be called to close websockets connection WebSocketProvider.prototype.unload = function() { this.ws.close(); }; + +/// Prototype object property +/// Should be used to set message handlers for this provider Object.defineProperty(WebSocketProvider.prototype, "onmessage", { set: function(provider) { this.onMessage(provider); } }); |