mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Cleanup
This commit is contained in:
@@ -40,7 +40,7 @@ function Server(remote, opts) {
|
|||||||
this.on('response_subscribe', function(message) {
|
this.on('response_subscribe', function(message) {
|
||||||
self._handle_response_subscribe(message);
|
self._handle_response_subscribe(message);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
util.inherits(Server, EventEmitter);
|
util.inherits(Server, EventEmitter);
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ Server.online_states = [
|
|||||||
|
|
||||||
Server.prototype._is_online = function (status) {
|
Server.prototype._is_online = function (status) {
|
||||||
return Server.online_states.indexOf(status) !== -1;
|
return Server.online_states.indexOf(status) !== -1;
|
||||||
}
|
};
|
||||||
|
|
||||||
Server.prototype._set_state = function (state) {
|
Server.prototype._set_state = function (state) {
|
||||||
if (state !== this._state) {
|
if (state !== this._state) {
|
||||||
@@ -76,7 +76,7 @@ Server.prototype._set_state = function (state) {
|
|||||||
this.emit('disconnect');
|
this.emit('disconnect');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
Server.prototype._remote_address = function() {
|
Server.prototype._remote_address = function() {
|
||||||
var address = null;
|
var address = null;
|
||||||
@@ -84,7 +84,7 @@ Server.prototype._remote_address = function() {
|
|||||||
address = this._ws._socket.remoteAddress;
|
address = this._ws._socket.remoteAddress;
|
||||||
}
|
}
|
||||||
return address;
|
return address;
|
||||||
}
|
};
|
||||||
|
|
||||||
Server.prototype.connect = function () {
|
Server.prototype.connect = function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
@@ -93,12 +93,18 @@ Server.prototype.connect = function () {
|
|||||||
// recently received a message from the server and the WebSocket has not
|
// recently received a message from the server and the WebSocket has not
|
||||||
// reported any issues either. If we do fail to ping or the connection drops,
|
// reported any issues either. If we do fail to ping or the connection drops,
|
||||||
// we will automatically reconnect.
|
// we will automatically reconnect.
|
||||||
if (this._connected === true) return;
|
if (this._connected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this._remote.trace) console.log('server: connect: %s', this._opts.url);
|
if (this._remote.trace) {
|
||||||
|
console.log('server: connect: %s', this._opts.url);
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure any existing socket is given the command to close first.
|
// Ensure any existing socket is given the command to close first.
|
||||||
if (this._ws) this._ws.close();
|
if (this._ws) {
|
||||||
|
this._ws.close();
|
||||||
|
}
|
||||||
|
|
||||||
// We require this late, because websocket shims may be loaded after
|
// We require this late, because websocket shims may be loaded after
|
||||||
// ripple-lib.
|
// ripple-lib.
|
||||||
@@ -111,20 +117,20 @@ Server.prototype.connect = function () {
|
|||||||
|
|
||||||
ws.onopen = function () {
|
ws.onopen = function () {
|
||||||
// If we are no longer the active socket, simply ignore any event
|
// If we are no longer the active socket, simply ignore any event
|
||||||
if (ws !== self._ws) return;
|
if (ws === self._ws) {
|
||||||
|
|
||||||
self.emit('socket_open');
|
self.emit('socket_open');
|
||||||
|
|
||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
var request = self._remote._server_prepare_subscribe();
|
var request = self._remote._server_prepare_subscribe();
|
||||||
self.request(request);
|
self.request(request);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onerror = function (e) {
|
ws.onerror = function (e) {
|
||||||
// If we are no longer the active socket, simply ignore any event
|
// If we are no longer the active socket, simply ignore any event
|
||||||
if (ws !== self._ws) return;
|
if (ws === self._ws) {
|
||||||
|
if (self._remote.trace) {
|
||||||
if (self._remote.trace) console.log('server: onerror: %s', e.data || e);
|
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
|
// code 1006. This is done for security purposes and therefore unlikely to
|
||||||
@@ -141,16 +147,18 @@ Server.prototype.connect = function () {
|
|||||||
// However, in Node.js this event may be triggered instead of the close
|
// However, in Node.js this event may be triggered instead of the close
|
||||||
// event, so we need to handle it.
|
// event, so we need to handle it.
|
||||||
handleConnectionClose();
|
handleConnectionClose();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Failure to open.
|
// Failure to open.
|
||||||
ws.onclose = function () {
|
ws.onclose = function () {
|
||||||
// If we are no longer the active socket, simply ignore any event
|
// If we are no longer the active socket, simply ignore any event
|
||||||
if (ws !== self._ws) return;
|
if (ws === self._ws) {
|
||||||
|
if (self._remote.trace) {
|
||||||
if (self._remote.trace) console.log('server: onclose: %s', ws.readyState);
|
console.log('server: onclose: %s', ws.readyState);
|
||||||
|
}
|
||||||
handleConnectionClose();
|
handleConnectionClose();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleConnectionClose() {
|
function handleConnectionClose() {
|
||||||
@@ -161,14 +169,19 @@ Server.prototype.connect = function () {
|
|||||||
ws.onopen = ws.onerror = ws.onclose = ws.onmessage = function () {};
|
ws.onopen = ws.onerror = ws.onclose = ws.onmessage = function () {};
|
||||||
|
|
||||||
// Should we be connected?
|
// Should we be connected?
|
||||||
if (!self._should_connect) return;
|
if (!self._should_connect) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Delay and retry.
|
// Delay and retry.
|
||||||
self._retry += 1;
|
self._retry += 1;
|
||||||
self._retry_timer = setTimeout(function () {
|
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;
|
}
|
||||||
|
if (!self._should_connect) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.connect();
|
self.connect();
|
||||||
}, self._retry < 40
|
}, self._retry < 40
|
||||||
? 1000/20 // First, for 2 seconds: 20 times per second
|
? 1000/20 // First, for 2 seconds: 20 times per second
|
||||||
@@ -182,7 +195,7 @@ Server.prototype.connect = function () {
|
|||||||
ws.onmessage = function (msg) {
|
ws.onmessage = function (msg) {
|
||||||
self.emit('message', msg.data);
|
self.emit('message', msg.data);
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
Server.prototype.disconnect = function () {
|
Server.prototype.disconnect = function () {
|
||||||
this._should_connect = false;
|
this._should_connect = false;
|
||||||
@@ -190,13 +203,13 @@ Server.prototype.disconnect = function () {
|
|||||||
if (this._ws) {
|
if (this._ws) {
|
||||||
this._ws.close();
|
this._ws.close();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
Server.prototype.send_message = function (message) {
|
Server.prototype.send_message = function (message) {
|
||||||
if (this._ws) {
|
if (this._ws) {
|
||||||
this._ws.send(JSON.stringify(message));
|
this._ws.send(JSON.stringify(message));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Submit a Request object to this server.
|
* Submit a Request object to this server.
|
||||||
@@ -229,19 +242,16 @@ Server.prototype.request = function (request) {
|
|||||||
self.send_message(request.message);
|
self.send_message(request.message);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else if (this._remote.trace) {
|
||||||
if (this._remote.trace) {
|
|
||||||
utils.logObject('server: request: DROPPING: %s', request.message);
|
utils.logObject('server: request: DROPPING: %s', request.message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Server.prototype._handle_message = function (json) {
|
Server.prototype._handle_message = function (json) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var message;
|
||||||
|
|
||||||
try {
|
try { message = JSON.parse(json); } catch(exception) { }
|
||||||
var message = JSON.parse(json);
|
|
||||||
} catch(exception) { return; }
|
|
||||||
|
|
||||||
if (typeof message !== 'object' || typeof message.type === 'undefined') {
|
if (typeof message !== 'object' || typeof message.type === 'undefined') {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user