mirror of
https://github.com/Xahau/xahau.js.git
synced 2026-06-03 16:56:41 +00:00
Set _connected
This commit is contained in:
@@ -15,51 +15,64 @@ var utils = require('./utils');
|
||||
@param cfg Configuration parameters.
|
||||
*/
|
||||
|
||||
var Server = function (remote, cfg)
|
||||
{
|
||||
var Server = function (remote, cfg) {
|
||||
EventEmitter.call(this);
|
||||
|
||||
if ("object" !== typeof cfg || "string" !== typeof cfg.url) {
|
||||
throw new Error("Invalid server configuration.");
|
||||
|
||||
if ('object' !== typeof cfg || 'string' !== typeof cfg.url) {
|
||||
throw new Error('Invalid server configuration.');
|
||||
}
|
||||
|
||||
this._remote = remote;
|
||||
this._cfg = cfg;
|
||||
var self = this;
|
||||
|
||||
this._ws = null;
|
||||
this._connected = false;
|
||||
this._remote = remote;
|
||||
this._cfg = cfg;
|
||||
|
||||
this._ws = void(0);
|
||||
this._connected = false;
|
||||
this._should_connect = false;
|
||||
this._state = null;
|
||||
this._state = void(0);
|
||||
|
||||
this._id = 0;
|
||||
this._retry = 0;
|
||||
this._id = 0;
|
||||
this._retry = 0;
|
||||
|
||||
this._requests = {};
|
||||
this._requests = { };
|
||||
|
||||
this.on('message', this._handle_message.bind(this));
|
||||
this.on('response_subscribe', this._handle_response_subscribe.bind(this));
|
||||
this.on('message', function(message) {
|
||||
self._handle_message(message);
|
||||
});
|
||||
|
||||
this.on('response_subscribe', function(message) {
|
||||
self._handle_response_subscribe(message);
|
||||
});
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
util.inherits(Server, EventEmitter);
|
||||
|
||||
function to_set(list) {
|
||||
var result = { };
|
||||
for (var i=0; i<list.length; i++) {
|
||||
result[list[i]] = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Server states that we will treat as the server being online.
|
||||
*
|
||||
* Our requirements are that the server can process transactions and notify
|
||||
* us of changes.
|
||||
*/
|
||||
Server.online_states = [
|
||||
Server.online_states = to_set([
|
||||
'syncing',
|
||||
'tracking',
|
||||
'proposing',
|
||||
'validating',
|
||||
'full'
|
||||
];
|
||||
]);
|
||||
|
||||
Server.prototype.connect = function ()
|
||||
{
|
||||
Server.prototype.connect = function () {
|
||||
var self = this;
|
||||
|
||||
// We don't connect if we believe we're already connected. This means we have
|
||||
@@ -68,7 +81,7 @@ Server.prototype.connect = function ()
|
||||
// we will automatically reconnect.
|
||||
if (this._connected === true) return;
|
||||
|
||||
if (this._remote.trace) console.log("server: connect: %s", this._cfg.url);
|
||||
if (this._remote.trace) console.log('server: connect: %s', this._cfg.url);
|
||||
|
||||
// Ensure any existing socket is given the command to close first.
|
||||
if (this._ws) this._ws.close();
|
||||
@@ -95,9 +108,9 @@ Server.prototype.connect = function ()
|
||||
// If we are no longer the active socket, simply ignore any event
|
||||
if (ws !== self._ws) return;
|
||||
|
||||
if (self._remote.trace) console.log("server: onerror: %s", e.data || e);
|
||||
if (self._remote.trace) console.log('server: onerror: %s', e.data || e);
|
||||
|
||||
// Most connection errors for WebSockets are conveyed as "close" events with
|
||||
// Most connection errors for WebSockets are conveyed as 'close' events with
|
||||
// code 1006. This is done for security purposes and therefore unlikely to
|
||||
// ever change.
|
||||
|
||||
@@ -119,13 +132,12 @@ Server.prototype.connect = function ()
|
||||
// If we are no longer the active socket, simply ignore any event
|
||||
if (ws !== self._ws) return;
|
||||
|
||||
if (self._remote.trace) console.log("server: onclose: %s", ws.readyState);
|
||||
if (self._remote.trace) console.log('server: onclose: %s', ws.readyState);
|
||||
|
||||
handleConnectionClose();
|
||||
};
|
||||
|
||||
function handleConnectionClose()
|
||||
{
|
||||
function handleConnectionClose() {
|
||||
self.emit('socket_close');
|
||||
self._set_state('offline');
|
||||
|
||||
@@ -138,7 +150,7 @@ Server.prototype.connect = function ()
|
||||
// Delay and retry.
|
||||
self._retry += 1;
|
||||
self._retry_timer = setTimeout(function () {
|
||||
if (self._remote.trace) console.log("server: retry");
|
||||
if (self._remote.trace) console.log('server: retry');
|
||||
|
||||
if (!self._should_connect) return;
|
||||
self.connect();
|
||||
@@ -156,11 +168,9 @@ Server.prototype.connect = function ()
|
||||
};
|
||||
};
|
||||
|
||||
Server.prototype.disconnect = function ()
|
||||
{
|
||||
Server.prototype.disconnect = function () {
|
||||
this._should_connect = false;
|
||||
this._set_state('offline');
|
||||
|
||||
if (this.ws) {
|
||||
this.ws.close();
|
||||
}
|
||||
@@ -169,8 +179,7 @@ Server.prototype.disconnect = function ()
|
||||
/**
|
||||
* Submit a Request object to this server.
|
||||
*/
|
||||
Server.prototype.request = function (request)
|
||||
{
|
||||
Server.prototype.request = function (request) {
|
||||
var self = this;
|
||||
|
||||
// Only bother if we are still connected.
|
||||
@@ -182,10 +191,10 @@ Server.prototype.request = function (request)
|
||||
// Advance message ID
|
||||
self._id++;
|
||||
|
||||
if (self._state === "online" ||
|
||||
(request.message.command === "subscribe" && self._ws.readyState === 1)) {
|
||||
if (self._state === 'online' ||
|
||||
(request.message.command === 'subscribe' && self._ws.readyState === 1)) {
|
||||
if (self._remote.trace) {
|
||||
utils.logObject("server: request: %s", request.message);
|
||||
utils.logObject('server: request: %s', request.message);
|
||||
}
|
||||
|
||||
self._ws.send(JSON.stringify(request.message));
|
||||
@@ -193,14 +202,14 @@ Server.prototype.request = function (request)
|
||||
// XXX There are many ways to make self smarter.
|
||||
self.once('connect', function () {
|
||||
if (self._remote.trace) {
|
||||
utils.logObject("server: request: %s", request.message);
|
||||
utils.logObject('server: request: %s', request.message);
|
||||
}
|
||||
self._ws.send(JSON.stringify(request.message));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (self._remote.trace) {
|
||||
utils.logObject("server: request: DROPPING: %s", request.message);
|
||||
utils.logObject('server: request: DROPPING: %s', request.message);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -210,9 +219,12 @@ Server.prototype._set_state = function (state) {
|
||||
this._state = state;
|
||||
|
||||
this.emit('state', state);
|
||||
|
||||
if (state === 'online') {
|
||||
this._connected = true;
|
||||
this.emit('connect');
|
||||
} else if (state === 'offline') {
|
||||
this._connected = false;
|
||||
this.emit('disconnect');
|
||||
}
|
||||
}
|
||||
@@ -230,15 +242,15 @@ Server.prototype._handle_message = function (json) {
|
||||
delete self._requests[message.id];
|
||||
|
||||
if (!request) {
|
||||
if (self._remote.trace) utils.logObject("server: UNEXPECTED: %s", message);
|
||||
if (self._remote.trace) utils.logObject('server: UNEXPECTED: %s', message);
|
||||
} else if ('success' === message.status) {
|
||||
if (self._remote.trace) utils.logObject("server: response: %s", message);
|
||||
if (self._remote.trace) utils.logObject('server: response: %s', message);
|
||||
|
||||
request.emit('success', message.result);
|
||||
self.emit('response_'+request.message.command, message.result, request, message);
|
||||
self._remote.emit('response_'+request.message.command, message.result, request, message);
|
||||
} else if (message.error) {
|
||||
if (self._remote.trace) utils.logObject("server: error: %s", message);
|
||||
if (self._remote.trace) utils.logObject('server: error: %s', message);
|
||||
|
||||
request.emit('error', {
|
||||
'error' : 'remoteError',
|
||||
@@ -248,20 +260,16 @@ Server.prototype._handle_message = function (json) {
|
||||
}
|
||||
} else if (message.type === 'serverStatus') {
|
||||
// This message is only received when online. As we are connected, it is the definative final state.
|
||||
self._set_state(
|
||||
Server.online_states.indexOf(message.server_status) !== -1
|
||||
? 'online'
|
||||
: 'offline');
|
||||
self._set_state(Server.online_states[message.server_status] ? 'online' : 'offline');
|
||||
}
|
||||
};
|
||||
|
||||
Server.prototype._handle_response_subscribe = function (message)
|
||||
{
|
||||
Server.prototype._handle_response_subscribe = function (message) {
|
||||
var self = this;
|
||||
|
||||
self._server_status = message.server_status;
|
||||
|
||||
if (Server.online_states.indexOf(message.server_status) !== -1) {
|
||||
if (Server.online_states[message.server_status]) {
|
||||
self._set_state('online');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user