providers documentation
This commit is contained in:
@ -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); }
|
||||
});
|
||||
|
Reference in New Issue
Block a user