mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 02:55:50 +00:00
JS: Automatically subscribe to server.
This commit is contained in:
25
js/remote.js
25
js/remote.js
@@ -189,6 +189,7 @@ Remote.prototype._set_state = function (state) {
|
|||||||
this.online_state = 'open';
|
this.online_state = 'open';
|
||||||
this.emit('connected');
|
this.emit('connected');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'offline':
|
case 'offline':
|
||||||
this.online_state = 'closed';
|
this.online_state = 'closed';
|
||||||
this.emit('disconnected');
|
this.emit('disconnected');
|
||||||
@@ -295,11 +296,11 @@ Remote.prototype._connect_start = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (self.online_target) {
|
if (self.online_target) {
|
||||||
if (self.trace) console.log("remote: onopen: %s: online_target2=%s", ws.readyState, self.online_target);
|
self._server_subscribe(); // Automatically subscribe.
|
||||||
|
|
||||||
self._set_state('online');
|
self._set_state('online');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (self.trace) console.log("remote: onopen: %s: online_target3=%s", ws.readyState, self.online_target);
|
|
||||||
self._connect_stop();
|
self._connect_stop();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -323,11 +324,12 @@ Remote.prototype._connect_start = function () {
|
|||||||
|
|
||||||
// Node's ws module doesn't pass arguments to onmessage.
|
// Node's ws module doesn't pass arguments to onmessage.
|
||||||
ws.on('message', function (json, flags) {
|
ws.on('message', function (json, flags) {
|
||||||
self._connect_message(json, flags);
|
self._connect_message(ws, json, flags);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Remote.prototype._connect_message = function (json, flags) {
|
// It is possible for messages to be dispatched after the connection is closed.
|
||||||
|
Remote.prototype._connect_message = function (ws, json, flags) {
|
||||||
var message = JSON.parse(json);
|
var message = JSON.parse(json);
|
||||||
var unexpected = false;
|
var unexpected = false;
|
||||||
var request;
|
var request;
|
||||||
@@ -339,7 +341,7 @@ Remote.prototype._connect_message = function (json, flags) {
|
|||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'response':
|
case 'response':
|
||||||
{
|
{
|
||||||
request = this.ws.response[message.id];
|
request = ws.response[message.id];
|
||||||
|
|
||||||
if (!request) {
|
if (!request) {
|
||||||
unexpected = true;
|
unexpected = true;
|
||||||
@@ -558,20 +560,23 @@ Remote.prototype.submit = function (transaction) {
|
|||||||
// Subscribe to a server to get 'ledger_closed' events.
|
// Subscribe to a server to get 'ledger_closed' events.
|
||||||
// 'subscribed' : This command was successful.
|
// 'subscribed' : This command was successful.
|
||||||
// 'ledger_closed : ledger_closed and ledger_current_index are updated.
|
// 'ledger_closed : ledger_closed and ledger_current_index are updated.
|
||||||
Remote.prototype.server_subscribe = function () {
|
Remote.prototype._server_subscribe = function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var request = new Request(this, 'server_subscribe');
|
var request = new Request(this, 'server_subscribe');
|
||||||
|
|
||||||
request.
|
request.
|
||||||
on('success', function (message) {
|
on('success', function (message) {
|
||||||
self.ledger_closed = message.ledger_closed;
|
|
||||||
self.ledger_current_index = message.ledger_current_index;
|
|
||||||
self.stand_alone = !!message.stand_alone;
|
self.stand_alone = !!message.stand_alone;
|
||||||
|
|
||||||
self.emit('subscribed');
|
if (message.ledger_closed && message.ledger_current_index) {
|
||||||
|
self.ledger_closed = message.ledger_closed;
|
||||||
|
self.ledger_current_index = message.ledger_current_index;
|
||||||
|
|
||||||
self.emit('ledger_closed', self.ledger_closed, self.ledger_current_index-1);
|
self.emit('ledger_closed', self.ledger_closed, self.ledger_current_index-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.emit('subscribed');
|
||||||
})
|
})
|
||||||
.request();
|
.request();
|
||||||
|
|
||||||
|
|||||||
@@ -213,6 +213,46 @@ buster.testCase("Remote functions", {
|
|||||||
})
|
})
|
||||||
.submit();
|
.submit();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"create account final" :
|
||||||
|
function (done) {
|
||||||
|
var got_proposed;
|
||||||
|
var got_success;
|
||||||
|
|
||||||
|
alpha.transaction()
|
||||||
|
.payment('root', 'alice', Amount.from_json("10000"))
|
||||||
|
.flags('CreateAccount')
|
||||||
|
.on('success', function (r) {
|
||||||
|
console.log("create_account: %s", JSON.stringify(r));
|
||||||
|
|
||||||
|
got_success = true;
|
||||||
|
})
|
||||||
|
.on('error', function (m) {
|
||||||
|
console.log("error: %s", m);
|
||||||
|
|
||||||
|
buster.assert(false);
|
||||||
|
})
|
||||||
|
.on('final', function (m) {
|
||||||
|
console.log("final: %s", JSON.stringify(m));
|
||||||
|
|
||||||
|
buster.assert(got_success && got_proposed);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.on('proposed', function (m) {
|
||||||
|
console.log("proposed: %s", JSON.stringify(m));
|
||||||
|
|
||||||
|
// buster.assert.equals(m.result, 'terNO_DST');
|
||||||
|
buster.assert.equals(m.result, 'tesSUCCESS');
|
||||||
|
|
||||||
|
got_proposed = true;
|
||||||
|
|
||||||
|
alpha.ledger_accept();
|
||||||
|
})
|
||||||
|
.on('status', function (s) {
|
||||||
|
console.log("status: %s", JSON.stringify(s));
|
||||||
|
})
|
||||||
|
.submit();
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// vim:sw=2:sts=2:ts=8
|
// vim:sw=2:sts=2:ts=8
|
||||||
|
|||||||
Reference in New Issue
Block a user