JS: Automatically subscribe to server.

This commit is contained in:
Arthur Britto
2012-10-17 15:37:23 -07:00
parent 9ced36eca6
commit b23f596e3f
2 changed files with 55 additions and 10 deletions

View File

@@ -189,6 +189,7 @@ Remote.prototype._set_state = function (state) {
this.online_state = 'open';
this.emit('connected');
break;
case 'offline':
this.online_state = 'closed';
this.emit('disconnected');
@@ -295,11 +296,11 @@ Remote.prototype._connect_start = function () {
};
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');
}
else {
if (self.trace) console.log("remote: onopen: %s: online_target3=%s", ws.readyState, self.online_target);
self._connect_stop();
}
};
@@ -323,11 +324,12 @@ Remote.prototype._connect_start = function () {
// Node's ws module doesn't pass arguments to onmessage.
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 unexpected = false;
var request;
@@ -339,7 +341,7 @@ Remote.prototype._connect_message = function (json, flags) {
switch (message.type) {
case 'response':
{
request = this.ws.response[message.id];
request = ws.response[message.id];
if (!request) {
unexpected = true;
@@ -558,20 +560,23 @@ Remote.prototype.submit = function (transaction) {
// Subscribe to a server to get 'ledger_closed' events.
// 'subscribed' : This command was successful.
// 'ledger_closed : ledger_closed and ledger_current_index are updated.
Remote.prototype.server_subscribe = function () {
Remote.prototype._server_subscribe = function () {
var self = this;
var request = new Request(this, 'server_subscribe');
request.
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.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();

View File

@@ -213,6 +213,46 @@ buster.testCase("Remote functions", {
})
.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