mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
JS: Get request-respons and ledger_current working.
This commit is contained in:
56
js/remote.js
56
js/remote.js
@@ -20,10 +20,10 @@ var Remote = function(trusted, websocket_ip, websocket_port, trace) {
|
|||||||
this.trace = trace;
|
this.trace = trace;
|
||||||
};
|
};
|
||||||
|
|
||||||
var remoteConfig = function(config, server) {
|
var remoteConfig = function(config, server, trace) {
|
||||||
var serverConfig = config.servers[server];
|
var serverConfig = config.servers[server];
|
||||||
|
|
||||||
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port);
|
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port, trace);
|
||||||
};
|
};
|
||||||
|
|
||||||
Remote.method('connect_helper', function() {
|
Remote.method('connect_helper', function() {
|
||||||
@@ -36,6 +36,8 @@ Remote.method('connect_helper', function() {
|
|||||||
|
|
||||||
var ws = this.ws;
|
var ws = this.ws;
|
||||||
|
|
||||||
|
ws.response = {};
|
||||||
|
|
||||||
ws.onopen = function() {
|
ws.onopen = function() {
|
||||||
if (this.trace)
|
if (this.trace)
|
||||||
console.log("remote: onopen: %s", ws.readyState);
|
console.log("remote: onopen: %s", ws.readyState);
|
||||||
@@ -80,23 +82,36 @@ Remote.method('connect_helper', function() {
|
|||||||
self.done(ws.readyState);
|
self.done(ws.readyState);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.onmessage) {
|
// XXX Why doesn't onmessage work?
|
||||||
ws.onmessage = this.onmessage;
|
ws.on('message', function(json, flags) {
|
||||||
}
|
var message = JSON.parse(json);
|
||||||
|
// console.log("message: %s", json);
|
||||||
|
|
||||||
|
if (message.type !== 'response') {
|
||||||
|
console.log("unexpected message: %s", json);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
var done = ws.response[message.id];
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
done(message);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.log("unexpected message id: %s", json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Target state is connectted.
|
// Target state is connectted.
|
||||||
// done(readyState):
|
// done(readyState):
|
||||||
// --> readyState: OPEN, CLOSED
|
// --> readyState: OPEN, CLOSED
|
||||||
Remote.method('connect', function(done, onmessage, timeout) {
|
Remote.method('connect', function(done, timeout) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port);
|
this.url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port);
|
||||||
this.done = done;
|
this.done = done;
|
||||||
|
|
||||||
if (onmessage)
|
|
||||||
this.onmessage = onmessage;
|
|
||||||
|
|
||||||
if (timeout) {
|
if (timeout) {
|
||||||
if (this.trace)
|
if (this.trace)
|
||||||
console.log("remote: expire: false");
|
console.log("remote: expire: false");
|
||||||
@@ -146,14 +161,15 @@ Remote.method('request', function(command, done) {
|
|||||||
|
|
||||||
ws.response[command.id] = done;
|
ws.response[command.id] = done;
|
||||||
|
|
||||||
ws.send(command);
|
if (this.trace)
|
||||||
|
console.log("remote: send: %s", JSON.stringify(command));
|
||||||
|
|
||||||
|
ws.send(JSON.stringify(command));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Request the current ledger.
|
// Get the current ledger entry (may be live or not).
|
||||||
// done(index)
|
Remote.method('ledger_current', function(done) {
|
||||||
// index: undefined = error
|
this.request({ 'command' : 'ledger_current' }, done);
|
||||||
Remote.method('ledger', function(done) {
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -166,20 +182,12 @@ Remote.method('submit', function(json, done) {
|
|||||||
// });
|
// });
|
||||||
});
|
});
|
||||||
|
|
||||||
// ==> entry_spec
|
|
||||||
Remote.method('ledger_entry', function(entry_spec, done) {
|
|
||||||
entry_spec.command = 'ledger_entry';
|
|
||||||
|
|
||||||
this.request(entry_spec, function() {
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// done(value)
|
// done(value)
|
||||||
// --> value: { 'status', status, 'result' : result, ... }
|
// --> value: { 'status', status, 'result' : result, ... }
|
||||||
// done may be called up to 3 times.
|
// done may be called up to 3 times.
|
||||||
Remote.method('account_root', function(account_id, done) {
|
Remote.method('account_root', function(account_id, done) {
|
||||||
this.request({
|
this.request({
|
||||||
'command' : 'ledger_entry',
|
'command' : 'ledger_current',
|
||||||
}, function() {
|
}, function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user