Add reconnect and reconnected events to ws
This commit is contained in:
@@ -50,7 +50,12 @@ Close the connection to the server.
|
|||||||
|
|
||||||
### Event: 'open'
|
### Event: 'open'
|
||||||
|
|
||||||
Emmited when the connection has been opened.
|
Emmited when the connection has been opened for the first time.
|
||||||
|
|
||||||
|
|
||||||
|
### Event: 'reconnected'
|
||||||
|
|
||||||
|
Emmited when the client has been opened after a reconnect.
|
||||||
|
|
||||||
|
|
||||||
### Event: 'update'
|
### Event: 'update'
|
||||||
@@ -78,7 +83,12 @@ Emited when the server responds to an operation sent by the client (usually afte
|
|||||||
|
|
||||||
### Event: 'close'
|
### Event: 'close'
|
||||||
|
|
||||||
Emitted when the connection has been closed.
|
Emitted when the connection has been finally closed, after a call to `ws.close()`
|
||||||
|
|
||||||
|
|
||||||
|
### Event: 'reconnect'
|
||||||
|
|
||||||
|
Emitted when the connection has been closed, but the client will try to reconnect.
|
||||||
|
|
||||||
|
|
||||||
### Event: 'error'
|
### Event: 'error'
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
_reconnect(timeout) {
|
_reconnect(timeout) {
|
||||||
this._teardown();
|
this._teardown();
|
||||||
this.readyState = READY_STATE_RECONNECTING;
|
if(this.readyState !== READY_STATE_CONNECTING) this.readyState = READY_STATE_RECONNECTING;
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.logger.info('Reconnecting to server', {category: 'bybit-ws'});
|
this.logger.info('Reconnecting to server', {category: 'bybit-ws'});
|
||||||
@@ -147,6 +147,9 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
if(this.readyState === READY_STATE_CONNECTING) {
|
if(this.readyState === READY_STATE_CONNECTING) {
|
||||||
this.logger.info('Websocket connected', {category: 'bybit-ws', livenet: this.options.livenet});
|
this.logger.info('Websocket connected', {category: 'bybit-ws', livenet: this.options.livenet});
|
||||||
this.emit('open');
|
this.emit('open');
|
||||||
|
} else if(this.readyState === READY_STATE_RECONNECTING) {
|
||||||
|
this.logger.info('Websocket reconnected', {category: 'bybit-ws', livenet: this.options.livenet});
|
||||||
|
this.emit('reconnected');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.readyState = READY_STATE_CONNECTED;
|
this.readyState = READY_STATE_CONNECTED;
|
||||||
@@ -177,6 +180,7 @@ module.exports = class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
if(this.readyState !== READY_STATE_CLOSING) {
|
if(this.readyState !== READY_STATE_CLOSING) {
|
||||||
this._reconnect(this.options.reconnectTimeout);
|
this._reconnect(this.options.reconnectTimeout);
|
||||||
|
this.emit('reconnect');
|
||||||
} else {
|
} else {
|
||||||
this.readyState = READY_STATE_INITIAL;
|
this.readyState = READY_STATE_INITIAL;
|
||||||
this.emit('close');
|
this.emit('close');
|
||||||
|
|||||||
Reference in New Issue
Block a user